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.
Code Java:
/*
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("?");
}
}
}
}
}
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.
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:
Code :
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;
}
}
}
}
}
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.