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:
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.