Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

java - Exception handling infinite loop

my question is short and sweet. I do not understand why my program infinitely loops when catching an error. I made a fresh try-catch statement but it looped and even copied, pasted and modified the appropriate variables from a previous program that worked. Below is the statement itself and below that will be the entire program. Thank you for your help!

try {
    input = keyboard.nextInt();
}
catch(Exception e) {
    System.out.println("Error: invalid input");
again = true;

}
if (input >0 && input <=10)
    again = false;

}

Program:

    public class Blanco {

    public static int input;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        nameInput();



    }

    /**
     *
     * @param name
     */
    public static void nameInput() {

        System.out.println("What is the name of the cartoon character : ");
        Scanner keyboard = new Scanner(System.in);
        CartoonStar star = new CartoonStar();
        String name = keyboard.next();
        star.setName(name);
        typeInput(keyboard, star);

    }

    public static void typeInput(Scanner keyboard, CartoonStar star) {

boolean again = true;
while(again){
        System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,
"
                + "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT");

try {
    input = keyboard.nextInt();
}
catch(Exception e) {
    System.out.println("Error: invalid input");
again = true;

}
if (input >0 && input <=10)
    again = false;

}


        switch (input) {
            case 1:
                star.setType(CartoonType.FOX);
                break;

            case 2:
                star.setType(CartoonType.CHICKEN);
                break;
            case 3:
                star.setType(CartoonType.RABBIT);
                break;
            case 4:
                star.setType(CartoonType.MOUSE);
                break;
            case 5:
                star.setType(CartoonType.DOG);
                break;
            case 6:
                star.setType(CartoonType.CAT);
                break;
            case 7:
                star.setType(CartoonType.BIRD);
                break;
            case 8:
                star.setType(CartoonType.FISH);
                break;
            case 9:
                star.setType(CartoonType.DUCK);
                break;
            case 10:
                star.setType(CartoonType.RAT);
                break;
        }
        popularityNumber(keyboard, star);
    }

    public static void popularityNumber(Scanner keyboard, CartoonStar star) {
        System.out.println("What is the cartoon popularity number?");
        int popularity = keyboard.nextInt();
        star.setPopularityIndex(popularity);
        System.out.println(star.getName() + star.getType() + star.getPopularityIndex());
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your program runs forever because calling nextInt without changing the state of the scanner is going to cause an exception again and again: if the user did not enter an int, calling keyboard.nextInt() will not change what the scanner is looking at, so when you call keyboard.nextInt() in the next iteration, you'll get an exception.

You need to add some code to read the garbage the user entered after servicing an exception to fix this problem:

try {
     ...
} catch(Exception e) {
    System.out.println("Error: invalid input:" + e.getMessage());
    again = true;
    keyboard.next(); // Ignore whatever is entered
}

Note: you do not need to rely on exceptions in this situation: rather than calling nextInt(), you could call hasNextInt(), and check if the scanner is looking at an integer or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...