Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.lang.NumberFormatException-三个异常_Java - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.NumberFormatException-三个异常

“线程中的异常”;“主要”;java.lang.NumberFormatException-三个异常,java,Java,这是我的代码,我不明白为什么会出现上述错误。它说它在第17行,这是一个开始作为int num import java.util.Scanner; public class Program5 { public static void main(String[] args) { // The driver class should instantiate a Verify object with a range of 10 to 100. Verify v

这是我的代码,我不明白为什么会出现上述错误。它说它在第17行,这是一个开始作为int num

import java.util.Scanner;

public class Program5 {

    public static void main(String[] args) {
        // The driver class should instantiate a Verify object with a range of 10 to 100.
        Verify verify = new Verify(10, 100);
        //It should then do the following:
        //Prompt the user to input a number within the specified range.
        System.out.print("Input a number between 10-100 inclusive: ");
        // Use a Scanner to read the user input as an int.
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.nextLine()); 
        try {
            //Call the validate method to validate that the number is within the range.
            verify.validate(num);
            //print the value if it is within the range. 
            System.out.println("Number entered: " + num);
        } catch (NumberNegativeException ex) {
            //Print an appropriate error message if the value is not within the range,
            System.out.println(ex.getMessage());
        } catch (NumberLowException ex) {
            //Print an appropriate error message if the value is not within the range,
            System.out.println(ex.getMessage());
        } catch (NumberHighException ex) {
            //Print an appropriate error message if the value is not within the range,
            System.out.println(ex.getMessage());
        } catch (NumberFormatException ex ) {
            System.out.println("You entered bad data." ); 
        }           
    }    
}
调用
nextInt()input
)上执行code>,而不是尝试解析该行。
或者将行
int num…
移动到try块中

那么…您输入的值是多少?@peter如果用户在数字前输入一堆空格,则可能是。请注意,这不会消耗输入的“下一行”字符。所以,如果他知道要注意这个问题,他将来可能会有问题。当然值得一提。