MountainDew7
Apr 7th, 2009, 08:11 PM
I'm fairly new to Java and I need help scrambling a word. I made the following but for some reason the if statement inside the do loop isn't working.
import java.util.Random;
class scramble {
public static void main(String[] args) {
String word,newword;
int rndnum;
Random randGen = new Random();
word = "tiger";
newword="";
boolean letter[] = new boolean[word.length()];
do {
rndnum = randGen.nextInt(word.length());
if (letter[rndnum] = false) {
newword = newword + word.charAt(rndnum);
letter[rndnum] = true;
}
} while (newword.length() < word.length());
System.out.println(newword);
}
}
Anyone know why? I printed the status of letter[rndnum] and it's always returning false, so I'm not sure why the if isn't working.
import java.util.Random;
class scramble {
public static void main(String[] args) {
String word,newword;
int rndnum;
Random randGen = new Random();
word = "tiger";
newword="";
boolean letter[] = new boolean[word.length()];
do {
rndnum = randGen.nextInt(word.length());
if (letter[rndnum] = false) {
newword = newword + word.charAt(rndnum);
letter[rndnum] = true;
}
} while (newword.length() < word.length());
System.out.println(newword);
}
}
Anyone know why? I printed the status of letter[rndnum] and it's always returning false, so I'm not sure why the if isn't working.