首页 > 解决方案 > Checking if a user input that should be an int is a string

问题描述

I'm trying to make this Sentinel program more robust by continuing it even when incorrect user inputs are received. I've gotten it to work if the user input is a different int, but if it is a string, or anything else really, the program crashes.

My current code attempt is this:

 } else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
            || userInt instanceof String) {

The first part of this code works fine at checking if the user input is a different in. The instanceof statement gives the error of "incompatible operand types int and String"

Should I even be using an instanceof statement? Is there a better way to check for this?

This is the whole method:

public static void printMenu() {

    Scanner userInput2 = new Scanner(System.in);

    String menu = new String("    Please choose from the following menu: \n    1. Rock paper Scissors\n    2. "
            + "Tip Calculator\n    3. "
            + "Number Adding\n    4. Guessing Game\n    5. Random\n    6. Exit");
    System.out.println(menu);
    int userInt = userInput2.nextInt();

    if (userInt == 1) {
        System.out.println("    You asked to play Rock Paper Scissors");
        System.out.println("    Launching Rock Paper Scissors... \n");
        RockPaperScissors gameRun1 = new RockPaperScissors();
        gameRun1.main(null);
    } else if (userInt == 2) {
        System.out.println("    You asked to run the Tip Calculator");
        System.out.println("    Launching the Tip Calculator... \n");
        TipCalculator gameRun2 = new TipCalculator();
        gameRun2.main(null);
    } else if (userInt == 3) {
        System.out.println("    You asked to run the Number Adding game");
        System.out.println("    Launching the Number Adding game... \n");
        NumberAddingGame gameRun3 = new NumberAddingGame();
        gameRun3.main(null);
    } else if (userInt == 4) {
        System.out.println("    You asked to play GuessingGame");
        System.out.println("    Launching GuessingGame... \n");
        GuessingGame gameRun4 = new GuessingGame();
        gameRun4.main(null);
    } else if (userInt == 5) {
            System.out.println("    You asked for a random game");
            option5();
    } else if (userInt == 6) {
            System.out.println(    "Thank you for using Conner's Sentinel");
            // figure out how to terminate the program from here

    } else if (userInt != 1 && userInt != 2 && userInt != 3 && userInt != 4 && userInt != 5 && userInt !=6
            || userInt instanceof String {
        System.out.println("Not a valid input, type 1-6");
        printMenu();
    }
    printMenu();
}

标签: java

解决方案


无法检查下一个输入是否int像您正在做的那样(只能userInput2.nextInt()返回一个int),而是您必须在分配结果之前进行检查。就像是,

if (userInput2.hasNextInt()) {
    int userInt = userInput2.nextInt();
    if (userInt == 1) {
        System.out.println("    You asked to play Rock Paper Scissors");
        System.out.println("    Launching Rock Paper Scissors... \n");
        RockPaperScissors gameRun1 = new RockPaperScissors();
        gameRun1.main(null);
    } else if (userInt == 2) {
        System.out.println("    You asked to run the Tip Calculator");
        System.out.println("    Launching the Tip Calculator... \n");
        TipCalculator gameRun2 = new TipCalculator();
        gameRun2.main(null);
    } else if (userInt == 3) {
        System.out.println("    You asked to run the Number Adding game");
        System.out.println("    Launching the Number Adding game... \n");
        NumberAddingGame gameRun3 = new NumberAddingGame();
        gameRun3.main(null);
    } else if (userInt == 4) {
        System.out.println("    You asked to play GuessingGame");
        System.out.println("    Launching GuessingGame... \n");
        GuessingGame gameRun4 = new GuessingGame();
        gameRun4.main(null);
    } else if (userInt == 5) {
        System.out.println("    You asked for a random game");
        option5();
    } else if (userInt == 6) {
        System.out.println("Thank you for using Conner's Sentinel");
        // figure out how to terminate the program from here
    } else {
        System.out.println("Not a valid input, type 1-6");
        printMenu();
    }
} else {
    userInput2.nextLine(); // <-- consume the non-number
    System.out.println("Not a valid number, type 1-6");
    printMenu();
}

推荐阅读