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 4 of 4

Thread: Prints an extra character for this game.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question Prints an extra character for this game.

    This program is supposed to work like hangman. My code is inputting extra '?' even after a user makes a correct guess. How do I correct this? I want to use the for loops that already exist and I am not supposed to use arrays as we have not covered that in class to this point. I need to get rid of the extra '?'s that are showing up. Does anyone have any ideas?

    Another error that pops up is if someone guesses a letter that has already been guessed, then the program inserts it a second time. I would like to know why, but it is not as important as solving the first problem.



    /*
    This program allows a user to input a phrase and has a second
    user try to guess the phrase.  A '?' will take the place of
    any letters not guessed while correct guesses will be placed
    in their correct index location.
    */
     
     
    import java.util.*;
    public class Program07
    {
    	public static void main(String[] args)
    	{
    		Scanner stdIn = new Scanner(System.in);
     
    		//variables
    		String cPhrase = "";
    		String guess = "";
     
    		//String to be guessed.  Input by user.  Forced to be lowercase.
    		System.out.println("Please enter a common phrase: ");
    		cPhrase = stdIn.nextLine().toLowerCase();
     
    		//Allow a user to make guesses
    		while (guess.length() < 35)
    		{
    			System.out.println('\n'+"Please enter a single character guess: "+ '\n');
    			guess += stdIn.next().charAt(0);
     
    				//Handling guesses
    				for (int j=0; j < cPhrase.length(); ++j)
    				{
    					for (int k=0; k < guess.length(); ++k)
    					{
    						if (guess.charAt(k) == cPhrase.charAt(j))
    						{
    							System.out.print(cPhrase.charAt(j));
    						}
     
    					}
     
    				if (cPhrase.charAt(j) == ' ') 
    				{
    					System.out.print(" ");
    				}
    				else
    				{
    					System.out.print("?");
    				}
    				}
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Prints an extra character for this game.

    I'm a little confused about what that code is supposed to do. What's the magic number 35? Are you reading in entire lines, or words, or characters? Are you sure? Hint: The enter key counts as an input character.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Prints an extra character for this game.

    Oh, the 35 is nothing significant. I was planning to replace that with a boolean to end the program when no more question marks were printed.

    I am reading an entire line with Scanner's .nextLine() to get String cPhrase // the string that will be guessed.

    After that, I am just using Scanner's .next().charAt(0) to grab char guess // the character guessed
    and then comparing that character to the characters in cPhrase.

    If the guess is correct at index 0, then print the guess and increment the for loop.
    If the guess is incorrect, print a question mark.
    If that spot is a space, just print a space.

    The behavior that occurs is the following:

    cPhrase = hello world
    guesses = hew

    output desired: he??? w????
    output occurring: h?e???? w?????
    where the underlined characters are extraneous.
    At the end the output would be "h?e?l?l?o? w?o?r?l?d?"

    I thought that .nextLine() uses a return carriage as an extra character, but not .next(). Could that be my problem?

    --- Update ---

    By the way, this is cleaned up a little more than the previous code, but the problem is still the same:

    import java.util.*;
    public class Program07
    {
    	public static void main(String[] args)
    	{
    		Scanner stdIn = new Scanner(System.in);
     
    		//variables
    		String cPhrase = "";
    		String guess = " ";
    		boolean finished = false;
     
    		//String to be guessed.  Input by user.  Forced to be lowercase.
    		System.out.println("Please enter a common phrase: ");
    		cPhrase = stdIn.nextLine().toLowerCase();
     
    		//Allow a user to make guesses
    		while (finished != true)
    		{
    			System.out.println('\n'+"Please enter a single character guess: "+ '\n');
    			guess += stdIn.next().charAt(0);
    			System.out.println("Letter already guessed: " + guess);
     
    				//Handling guesses
    				for (int j=0; j < cPhrase.length(); ++j)
    				{
    					for (int k=0; k < guess.length(); ++k)
    					{
    						if (guess.charAt(k) == cPhrase.charAt(j))
    						{
    							System.out.print(cPhrase.charAt(j));
    						}
     
    					}
    					if (cPhrase.charAt(j) == ' ')
    					{
    						System.out.print(' ');
    					}
    					else if (cPhrase.charAt(j) != cPhrase.indexOf(j)) 
    					{
    						System.out.print("?");
    					}
    					else
    					{
    						//finished = true;
    					}
     
    				}
     
    		}
    	}
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Prints an extra character for this game.

    I would take a look at what's being held by your guess variable. You're only ever reading in one token at a time- what's happening to the end statements?

    But really, I would probably just use the String.repalceAll() function for this.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Video Game Character ArrayList Help?
    By MagicTricksKill in forum Object Oriented Programming
    Replies: 0
    Last Post: October 15th, 2012, 03:44 PM
  2. [SOLVED] Extra spaces in output - why & how to fix?
    By KL1209 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 7th, 2012, 11:44 PM
  3. Double to string giving extra values
    By sinsand in forum Collections and Generics
    Replies: 1
    Last Post: August 23rd, 2011, 05:57 AM
  4. Cannot find where extra brace is at causing my errors
    By eagle09 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 27th, 2011, 07:30 PM
  5. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM