PDA

Click to See Complete Forum and Search --> : Help scrambling a word


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.

ComputerJy
Apr 8th, 2009, 01:15 AM
"letter[rndnum] = false" This line sets the array element to false and of course it's a false condition, so the code within the if is never executed

MountainDew7
Apr 8th, 2009, 05:55 AM
Oh I see, it should be ==...