0

I am learning java and am making a hangman game but this one line seems to just be ignored and not ran. if i put the same bit of code further up then it is ran, it just isn't ran while its its the while loop. (the "char guess = reader.nextLine().charAt(0);" isn't ran and no input is requested). edit: i have added a reader.nextLine(); after the int input but it still doesnt work.

error:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at test.Testtt.main(Testtt.java:30)

code:

public class Testtt {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        print("Would you like to play singleplayer or multiplayer (s/m)? ");
        String mode = reader.nextLine();

        print("How many game would you like to play?");
        int game_num = reader.nextInt();
        reader.nextLine();

        int score = 0;

        for(int x = 0; x < game_num; x++) {
            String word = get_word(mode);
            String temp = get_temp(word);
            int lives = 6;
            String[] guesses = new String[26];

            while(lives > 0) {
                print("Enter letter");
                char guess = reader.nextLine().charAt(0);
                temp = update_temp(temp, word, guess);

                if(win_check(temp, word)) {
                    score = score + 1;
                    break;

                }
                else {
                    lives = lives - 1;
                    display(lives);
                }
            }
        }
    }
  • their solutions don't work, this isn't a duplicate – Regi Vaineikis Jul 15 '18 at 11:01
  • This is not a duplicate indeed. NoSuchElementException from a Scanner means you've reached end of input the scanner was reading. Since you built the scanner on System.in, it means the standard input of the program reached its end. That can happen if you made your program read its standard input from a file, but in that case, why didn't you say so? How do you expect to receive help when you hide very important information. Or that can happen when you run the program normally but you choose to input the console key sequence to terminate the standard input. Why would you do that? – kumesana Jul 15 '18 at 14:08

0 Answers0