“线程中的异常”;“主要”;java.lang.NumberFormatException-三个异常
这是我的代码,我不明白为什么会出现上述错误。它说它在第17行,这是一个开始作为int num“线程中的异常”;“主要”;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
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如果用户在数字前输入一堆空格,则可能是。请注意,这不会消耗输入的“下一行”字符。所以,如果他知道要注意这个问题,他将来可能会有问题。当然值得一提。