Results 1 to 3 of 3

Thread: Help scrambling a word

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Help scrambling a word

    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.

    Code:
    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.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Help scrambling a word

    "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
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Re: Help scrambling a word

    Oh I see, it should be ==...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width