线程" main"中的例外情况java.lang.NumberFormatException - 三个例外

时间:2013-12-29 20:01:48

标签: 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 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." ); 
        }           
    }    
}

1 个答案:

答案 0 :(得分:0)

在扫描程序(nextInt();)上调用input,而不是尝试解析该行。 或者,在try块

中移动行int num ...