-2

When running it catches exceptions properly but when i choose a a case it causes an infinite loop. I have tried for hours looking for a solution and nothing has helped.

int whatYouWant = 1;
boolean contin = true;
while (whatYouWant != 0) {
    while (contin) {
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("Which program would you like to run?" + " Input 0 to exit the program.");
            whatYouWant = scanner.nextInt();
            contin = false;
            scanner.close();
        } catch (Exception e) {
            System.out.println("Try again.");
            contin = true;
        }
    }
    AllCode a = new AllCode();
    switch (whatYouWant) {
    case 1:
        // method call that calls a method with another try catch block
        break;
    case 2:
        // another method call
        break;
    case 3:
        // another method call and so on and so forth
        break;
    case 0:
        whatYouWant = 0;
        break;
    default:
        System.out.println("Please try again.");
        break;
    }
}

Here is an example of a method that the switch calls:

void multiplytheNumbers() {// Using the parameter to

    boolean run = true;
    while (run) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the 1st number:");
            int num1 = sc.nextInt(); /// getting user input from the user.
            System.out.println("Enter the 2nd number:");
            int num2 = sc.nextInt(); /// getting more user input
            int product = num1 * num2; /// using the multiplication operator
            int sum = num1 + num2; /// using then addition operator
            int diff = num1 - num1; /// using the subtraction operator
            int quotient = num1 / num1; /// using the quotient operator
            System.out.println(sum);
            System.out.println(diff);
            System.out.println(product);
            System.out.println(quotient);
            num1 = num1++; // using the increment operator to increase num1
                            // by 1
            num2 = num2--; // using the decrement operator to decrease num2
                            // by 1
            run = false;
            sc.close();
        } catch (Exception e) {
            System.out.println("Invalid Input. Try again.");
        }
    }
}
Harsh Poddar
  • 2,136
  • 15
  • 17
c. Burg
  • 1
  • 1
  • which one of your loop is infinite? – Abdelhak Apr 17 '16 at 14:44
  • 2
    Don't use try-catch as main part of your control flow. If you want to prevent user from providing incorrect values simply check if scanner `hasNextInt` (if it doesn't inform user about it and consume incorrect token using `next`). More info: [Validating input using java.util.Scanner](http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) (possibly duplicate). – Pshemo Apr 17 '16 at 14:47
  • 1
    I suggest you use you debugger to step through the code and see why it is going into an infinite loop. Using a debug you should be able to solve this in a couple of minutes once you know how to use it. – Peter Lawrey Apr 17 '16 at 14:47

2 Answers2

0

So this is because if Scanner.nextInt() does not match an int, it throws an exception but if you call Scanner.next(), you'll see the same string that you put in earlier. So every time you go into a loop, it tries to parse the same string input as an Integer and throws an exception every time.

You can notice this behavior by the following code:

Scanner scanner = new Scanner(System.in);
while (contin) {
    try {
        System.out.println("Which program would you like to run?" + " Input 0 to exit the program.");
        whatYouWant = scanner.nextInt();
        contin = false;
    } catch (Exception e) {
        System.out.println(scanner.next());
        System.out.println("Try again.");
        contin = true;
    }
}
scanner.close();

You will notice that it will still scan the same string when called scanner.next().

For a quick solution, you should try Integer.parseInt(scanner.next()); in place of scanner.nextInt();

Harsh Poddar
  • 2,136
  • 15
  • 17
-1

I'm reading a defualt instead of default in your switch.

Dinsdale
  • 154
  • 4
  • 10
  • 1
    yeah its fine in my actual program I just rewrote the switch statement and that was a typo. Definitely not the issue. – c. Burg Apr 17 '16 at 14:49
  • That should be a comment. Even if that would be an issue, questions about typos are off-topic on Stack Overflow so proper way to help OP would be posting it as comment. If you post it as answer ant it will get upvote or be accepted automatic cleanup script will not delete such question anymore. – Pshemo Apr 17 '16 at 14:51