brainfrz icon

Untitled

brainfrz | PRO | 09/26/15 05:17:37 AM UTC | 0 ⭐ | 870 👁️ | Never ⏰ | []
Java |

890 B

|

None

|

0 👍

/

0 👎

// Hmm, why is the first one fine, but he second one causing an infinite loop when failing?
 
// First one
String number;
int highestFactor;
do
{
    System.out.println("Please enter how high you'd like to multiply (up to 20):  ");
    number = user_input.next();
 
    try {
        highestFactor = Integer.parseInt(number);
    } catch (NumberFormatException e) {
        System.out.print("That's not a number! ");
        highestFactor = -1;
    }
} while (highestFactor <= 0 || highestFactor > MAX_FACTOR);
 
// Second one
int highestFactor;
do
{
    try {
        System.out.println("Please enter how high you'd like to multiply (up to 20):  ");
        highestFactor = user_input.nextInt();
    } catch (InputMismatchException e) {
        System.out.print("That's not a number! ");
        highestFactor = -1;
    }
} while (highestFactor <= 0 || highestFactor > MAX_FACTOR);

Comments