3

I need help Using Exception handling with Wrong User Input. I am creating a text based game that welcomes the User and then goes to the main menu. It then tell the User the options, and then look for User input. For some Reason, whenever I input 1 or 2, It says: "Your input is invalid, please try again" And goes back to the choices. I don't know exactly where I am going wrong, hopefully someone can help me. Also, it won't catch the Mismatch Exception either. Hope you can Help! Thanks, Shandan

   public static void main(String[] args) {



    System.out.println("Welcome to Spec Ops!");
    System.out.println("Please state your name:");
    Scanner name = new Scanner(System.in);
    String Name = name.next();

    System.out.println("Hello "+Name);
    mainMenu();

    }


public static void mainMenu() {

    System.out.println("1. Story Mode");
    System.out.println("2. Infinant Combat");



    Scanner input = new Scanner(System.in);



    Object Selection = input.nextInt();

    boolean validOption = true;
    Integer x;


    try {
    x = (Integer)Selection;
    } catch(ClassCastException cce){
        System.out.println("Your input is invalid, please try again");
        validOption = false;
    } catch(InputMismatchException ime){
        System.out.println("Your input is invalid, please try again");
        validOption = false;

    }


if(validOption) {
    System.out.println("Hello!");
}

else {
    mainMenu();
}

}

}

1
  • 6
    Any specific reason why you are not doing: - int selection = input.nextInt();? Commented Dec 20, 2012 at 19:06

3 Answers 3

8

Scanner.nextInt returns ant int, so there is no need to go Object selection = scanner.nextInt(), and then cast to an int, you can merely have int selection = scanner.nextInt() and surround that in a try catch that chatches java.util.InputMismatchException, which is the exception thrown when the user types a letter and not an number

Sign up to request clarification or add additional context in comments.

Comments

1

You can temporarily change your code and use the Pokemon exception handling and check the stack trace to see what kind of exception you should implement:

try {
    //do something
} catch(Exception e) {
    //Gotta catch 'em all!
    e.printStackTrace();
}

Once you know the exact exception, put some breakpoints and refactor your code accordingly.

Also, you don't have to control the flow of your program by changing your boolean variable inside the catch blocks:

boolean isValidOption = false;
Integer x;

try {
     x = (Integer)Selection;
     isValidOption = true;
} catch...

Comments

1

When you read the name in you are using scanner.next(). This does not read in the end of line character, so when you call scanner.nextInt() it reads the new line and fails to parse as an int.

You should change the name reading to scanner.nextLine() and if you want to handle invalid int inputs from the user, the call to nextInt needs to be in your try block and catch the InputMismatchException instead of the ClassCastException.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.