5

I'm new to Java and I wanted to keep on asking for user input until the user enters an integer, so that there's no InputMismatchException. I've tried this code, but I still get the exception when I enter a non-integer value.

int getInt(String prompt){
        System.out.print(prompt);
        Scanner sc = new Scanner(System.in);
        while(!sc.hasNextInt()){
            System.out.println("Enter a whole number.");
            sc.nextInt();
        }
        return sc.nextInt();
}

Thanks for your time!

4

5 回答 5

10

next使用而不是获取输入nextInt。使用 parseInt 方法放置一个 try catch 来解析输入。如果解析成功,则中断 while 循环,否则继续。尝试这个:

        System.out.print("input");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Enter a whole number.");
            String input = sc.next();
            int intInputValue = 0;
            try {
                intInputValue = Integer.parseInt(input);
                System.out.println("Correct input, exit");
                break;
            } catch (NumberFormatException ne) {
                System.out.println("Input is not a number, continue");
            }
        }
于 2013-10-02T04:58:21.800 回答
7

更短的解决方案。只需在 sc.next() 中输入

 public int getInt(String prompt) {
    Scanner sc = new Scanner(System.in);
    System.out.print(prompt);
    while (!sc.hasNextInt()) {
        System.out.println("Enter a whole number");
        sc.next();
    }
    return sc.nextInt();

}
于 2013-10-02T05:28:46.323 回答
5

使用 Juned 的代码,我能够将其缩短。

int getInt(String prompt) {
    System.out.print(prompt);
    while(true){
        try {
            return Integer.parseInt(new Scanner(System.in).next());
        } catch(NumberFormatException ne) {
            System.out.print("That's not a whole number.\n"+prompt);
        }
    }
}
于 2013-10-02T06:03:59.453 回答
1

在仍有输入的同时继续轻轻扫描,并根据需要检查它是否确实是整数:

String s = "This is not yet number 10"; 
  
        // create a new scanner 
        // with the specified String Object 
        Scanner scanner = new Scanner(s); 
  
        while (scanner.hasNext()) { 
  
            // if the next is a Int, 
            // print found and the Int 
            if (scanner.hasNextInt()) { 
                System.out.println("Found Int value :"
                                   + scanner.nextInt()); 
            } 
  
            // if no Int is found, 
            // print "Not Found:" and the token 
            else { 
                System.out.println("Not found Int value :"
                                   + scanner.next()); 
            } 
        } 
        scanner.close();
于 2020-09-28T15:39:29.157 回答
0

作为替代方案,如果它只是一个数字整数 [0-9],那么您可以检查它的 ASCII 码。它应该是 48-57 之间的整数。

基于 Juned 的代码,您可以用 if 条件替换 try 块:

    System.out.print("input");
    Scanner sc = new Scanner(System.in);
    while (true) {
            System.out.println("Enter a whole number.");
            String input = sc.next();
            int intInputValue = 0;
            if(input.charAt(0) >= 48 && input.charAt(0) <= 57){
                System.out.println("Correct input, exit");
                    break;
            }
            System.out.println("Input is not a number, continue");
    }
于 2013-10-02T05:27:38.843 回答