-2
import java.util.*;

public class NGG {
    static Scanner numberEntered;
    static Scanner userInput = new Scanner(System.in);
    static int guessedNumber;
    static int randomNumber = (int) (Math.random()* 11);
    static Scanner reply;
    static String answer;

    public static void main(String[] args) {
        guessChecker(guessedNumber);
    }

    public static void guessChecker(int userGuess) {
        while (userGuess != randomNumber) {
            intro();
            userGuess = intChecker();
            if (userGuess == randomNumber) {
                System.out.println("Congradulations!");
            System.exit(0);                     
            } else {
                System.out.println("That was Incorrect!");
                delay(1000);
                retryChecker(reply, answer);
            }
        }
    }

    public static int intChecker() {
        try {
            return userInput.nextInt(); 
        } catch (InputMismatchException e) {
            userInput.next();       
            System.out.println("Your answer was Invalid!");
            delay(2000);
            retryChecker(reply, answer);
            return 0;
        }
    }

    public static void retryChecker(Scanner userReply, String userChoice) {
        System.out.println("Would you like to try again?");
        userReply = new Scanner(System.in);
        userChoice = userReply.nextLine(); 
        if (userChoice.equalsIgnoreCase("yes") || userChoice.equalsIgnoreCase("y")) {
            guessChecker(guessedNumber);    
        } else {
            System.exit(0);
        }
    }

    public static void intro() {
        System.out.println("I'm thinking of a number in my head...");
        delay(1000);
        System.out.print("Try to guess it: ");
    }

    public static void delay(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {}
    }    
}

and here's my problem:

enter image description here

I have a number guessing game, every time it says "try to guess it:" it usually will let you type in a guess, unless your previous guess was a string, letter or number, followed by a space and then another string, letter or number, then instead of letting you write your own guess it will just print out "your answer was Invalid" and move on with the program.

How would I fix this? so that the userInput can also be a string, letter or number, followed by a space and then another string, letter or number and it'll move one with the program normally.

Matthew Brzezinski
  • 1,493
  • 3
  • 26
  • 47
Zaid A
  • 25
  • 11
  • can you be more specific – Zaid A Nov 17 '16 at 20:20
  • Well ^^ That is the issue, but `static int guessedNumber`, sets `guessedNumber` to 0, and you never update that variable. – OneCricketeer Nov 17 '16 at 20:21
  • can you show me how to fix it in my code. This is my first time making a java program and everything is very confusing to me – Zaid A Nov 17 '16 at 20:28
  • Recommendations: Start over. Start smaller, not so many methods. Use one `static Scanner` variable always. Never make `new Scanner` after that one. Then, read that post on how `nextLine()` conflicts with the usage of `next()` and `nextInt()` – OneCricketeer Nov 17 '16 at 20:40
  • Also, download an IDE instead of using a command-line. That way, you can "debug" and "step through" the code. – OneCricketeer Nov 17 '16 at 20:41

1 Answers1

0

The problem is in the intChecker().

public static int intChecker() {
        try {
            return userInput.nextInt(); 
        } catch (InputMismatchException e) {
            userInput.nextLine(); // -> changed from .next() to nextLine()      
            System.out.println("Your answer was Invalid!");
            delay(2000);
            retryChecker(reply, answer);
            return 0;
        }
    }

The reason is when you use next() it returns string when it encounters space or EOF.

So when you give input it's me!, first checks it's and says it's wrong. It asks whether to continue or not next. When you press y to goes to the method and reads the remaining string me!.

Here you have used different scanners userInput and userReply. As the userInput is static, the object doesn't dies and has the remaining string in it which is me! after returning its.

So using nextLine() returns the whole string.

For more info on how they both work, check out my other answer

I hope it helped.

Community
  • 1
  • 1
SkrewEverything
  • 2,133
  • 1
  • 15
  • 39
  • @ZaidA I can't tell what's wrong with your code if you say you didn't get. You should tell specifically. P.S. Everything runs fine on my pc without any error. If it says `cannot find symbol` then you must have using a new variable without declaring it. – SkrewEverything Nov 17 '16 at 22:18