Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help - easy question (I think)

Status
Not open for further replies.

OverkillMonkey

Programmer
May 24, 2001
9
US
import java.math.*;
import java.util.*;

public class genes {
char geenes[];
int geen = 0;
double tempgene = 0;

public void randgenes() {
for (int i=0; i=256; i++) {
tempgene = (Math.random());
geen = (int)tempgene*100;
geenes= (char)geen;

}
}
}

this is giving me an error - "genes.java": Error #: 354 : incompatible types; found: int, required: boolean at line 10, column 19
 
This is a common problem and is usually a typo but sometimes not.

Remember "=" means assignment and "==" means equivilence. E.g.:

x=5; // Assign the value of 5 to the variable x

if (x == 5) {...} // If the value of x is equivilent to 5...
 
You need initialize the char array first and then assign the values with geenes= (char)geen;
initialize geenes array like this:
geenes = new char[size of array];
 
Try this program: which i modified to see the results.

Remember that: random() function in java returns a pseudo random number between 0 & 1.

2. when using arrays initialize them before using.
3. There's explicit downcasting required for converting from int to char but not to char[], that's your problem.

4. Although none of the conditions are required in a for loop; when specified must evaluate to boolean expression.

5. Using String could be more useful instead of char[] is in C.

I have the code below and saved as genes.java, try it and it should work.


import java.math.*;
import java.util.*;

public class genes {
char geenes;
int geen = 0;
double tempgene = 0;

public static void main(String[] args){

genes g = new genes();
g.randgenes();

}
public void randgenes() {
for (int i=0; i<5; i++) {
tempgene = (Math.random());
System.out.println(&quot;tempgene is&quot; + tempgene);
geen = (int)tempgene*100;
System.out.println(&quot;geen is&quot; + geen);
geenes= (char)geen;
System.out.println(&quot;char is &quot; + geenes);
}
}
}

Hope that helps.


KSPR-
SCJP2.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top