0

i'm essentiel trying to create a movie guessing game, a bit like hangman, but you need to guess a movie title. The movie title is encoded with each character being represented by the underscore ('_') character. As the user inputs a correct letter, the underscore at the position of the corresponding letter changes to that letter. The user inputs his guesses as single chars using the scanner. My problem is that some movie titles contain a space (' '). But I am unable to use the scanner to return a space... My guess is that a scanner uses the whitespace as the default delimiter therefore it doesn't return a space as a character because it uses the whitespace to break the input into tokens?

By 'return a space' I mean consider a space as a input, and not as a delimiter.

Can anyone give me a solution as to how I could detect a space from the user input?

This code should read through the movieTitle, then if the movie title contains a space at index i, and the user input is a space, then the codedMovieTitle should update with a space at position i, instead of an underscore. However this doesn't work. When I enter a space using the scanner nothing happens...

    for (int i = 0; i < movieTitle.length(); i++) {       
            if(Character.isSpaceChar(movieTitle.charAt(i)) && 
            Character.isSpace(userInput)){
                    encodedMovieTitle = encodedMovieTitle.substring(0, i)
                    + userInput
                    + encodedMovieTitle.substring(i + 1);
            }
    }

This is the code for my scanner:

    Scanner scanner = new Scanner(System.in);
    char userInput;
    while(!gameIsWon && (numberOfGuessesLeft != 0)) {
        while (scanner.hasNextInt()) {
            System.out.println("You must enter a single character, try again.");
            scanner.next();
        }
        userInput = scanner.nextLine().charAt(0);

I am also struggling with enabling my scanner input to be non case sensitive. Some of the movies have capital letters in them. If the title is "Snowstorm" and the user inputs the character 's', the resulting encodedMovieTitle will be "_ _ _ _ s _ _ _ _", not "S _ _ _ s _ _ _ _"

If anyone has a solution that would be great! Most solutions I found involved using strings but I use chars as my userInput etc therefore I was wondering if there's a solution involving chars.

Code for checking is userInput (char) is in movieTitle (string):

    for (int i = 0; i < movieTitle.length(); i++) {
            if (updatedCodedMovieTitle.charAt(i) != userInput && movieTitle.charAt(i) == userInput) {
                updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                                        + userInput
                                        + updatedCodedMovieTitle.substring(i + 1);
                wordHasBeenUpdated = true;
            }
            if(Character.isSpaceChar(movieTitle.charAt(i)) && Character.isSpaceChar(userInput)){
                updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                        + " "
                        + updatedCodedMovieTitle.substring(i + 1);
            }
        }
CheckCho
  • 17
  • 2

3 Answers3

1

You can take input from the scanner in the following manner:

Scanner scanner = new Scanner(System.in);
String str;
str = scanner.nextline();

This will accept the space as a character not as a delimiter.

Hope this helps:)

Amrit Singh
  • 98
  • 1
  • 7
0

Indeed, Scanner (documentation)

breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

And whitespace includes space too.

But you can easily modify Scanner's delimiter pattern using its useDelimiter method, e.g.:

Scanner s = new Scanner(System.in);
s.useDelimiter("\n");

\n is the newline character, which in this case means that Scanner will generate new tokens when user presses <Enter>. So spaces will be returned as expected.

Or if you want Scanner to return only 1 character at a time, you can set "" as the delimiter, and then use s.next() to get a character from the input:

Scanner s = new Scanner(System.in);
s.useDelimiter("");
String input = s.next(); // this will be only one character
juzraai
  • 4,835
  • 8
  • 26
  • 41
  • Glad to help :) If this answer solved your problem, please mark it as accepted by clicking the checkmark next to the answer. [(more information on accepting)](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – juzraai Aug 30 '17 at 16:28
0

To match the strings regardless of cases you can use equalsIgnoreCase() as:

String movieName = "Snowstrom";
if(str.equalsIgnoreCase(movieName.charAt(0))) {
    // Set the value at appropriate location, 0 in this case
}

The statement in the if condition will compare the two strings regardless of their cases. The above condition will evaluate to true whether the user enters s or S.

UPDATE

You can take input for the character as follows:

Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\n");
char userInput;
userInput = scanner.next().charAt(0);

The following is your code with one modification:

for (int i = 0; i < movieTitle.length(); i++) {
    if (updatedCodedMovieTitle.charAt(i) != userInput && (movieTitle.charAt(i) == userInput || movieTitle.charAt(i) == Character.toUpperCase(userInput))) {
        updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                                        + userInput
                                        + updatedCodedMovieTitle.substring(i + 1);
        wordHasBeenUpdated = true;
    }
    if(Character.isSpaceChar(movieTitle.charAt(i)) && Character.isSpaceChar(userInput)){
        updatedCodedMovieTitle = updatedCodedMovieTitle.substring(0, i)
                        + " "
                        + updatedCodedMovieTitle.substring(i + 1);
    }
}

The change I made in your code is in the first if condition to compare both the uppercase and lowercase characters. I tested this program myself and it worked fine for me and giving me the desired output.

Hope this works out for you also:)

Amrit Singh
  • 98
  • 1
  • 7