Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help come out of inifinite loop

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help come out of inifinite loop

    Trying to code a Hangman Console Game. But my loop is not stopping when two words are equal. Below is my code. The secret word is "hello". Outcome is under code.

    import java.util.Scanner; 
    public class Hangman
    {
    	public static void main(String[] args)
    	{	
    		int numGuesses = 1;
    		String word;
    		String displayHidden = "";
    		String used = "";
    		Game myWord = new Game();
    		char guess;
     
    		word = myWord.getWord();
     
    		for (int i = 0; i < word.length(); i++)
            {
                displayHidden += "*";
            } 
     
    		StringBuilder guessedWord = new StringBuilder(word);
    		StringBuilder test = new StringBuilder(displayHidden);
     
    		System.out.println("Welcome to Hangman... v1.0 - Designed Ashley Bwanya");
    		System.out.println("This is the secret word you have to guess: \n");
    	   System.out.println(test+ "\n");
     
    		  do
    		  {  
    			 System.out.print("Guess Letter: ");
              Scanner scan = new Scanner(System.in);
              guess = scan.next().charAt(0);
              for(int i = 0; i < guessedWord.length(); i++){        //check if letter has been guessed
              if(guessedWord.charAt(i) == guess)
    			 {       
    			 		test.setCharAt(i, guess);
    					System.out.println(test);
     
    			 }
     
    			   											          }
    			}while(guessedWord.equals(test) == false);
     
    		}
     
    }
    OUTPUT

    Welcome to Hangman... v1.0 - Designed Ashley Bwanya
    This is the secret word you have to guess:

    *****

    Guess Letter: h
    h****
    Guess Letter: e
    he***
    Guess Letter: l
    hel**
    hell*
    Guess Letter: o
    hello
    Guess Letter:
    Last edited by jps; November 3rd, 2012 at 01:04 PM. Reason: added code tags


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help come out of inifinite loop

    A StringBuilder object will not be equal to a different StringBuilder instance, so the .equals() test will always be false in your program.

    You want to compare two Strings contained by the StringBuilder objects for equality, so what you need is to use the String class equals() method to compare guessedWord.toString() and test.toString()

    In fact, since your String "word" does not change, you don't need the StringBuilder guessedWord at all.

    Just use the equals() method to compare the String word with test.toString()

    Cheers!

    Z

  3. #3
    Banned
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help come out of inifinite loop

    just use the below line for your while condition
    //removed spoonfed code\\
    it will work.
    enjoy...
    Last edited by jps; November 3rd, 2012 at 01:02 PM. Reason: removed code

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help come out of inifinite loop

    @Karibasappa Please read The problem with spoonfeeding

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Help come out of inifinite loop

    Quote Originally Posted by Karibasappa View Post
    just use the below line for your while condition...
    while (something == false)

    is ugly code. Much cleaner is to do

    while (!something)

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM

Tags for this Thread