user2161312 user2161312 - 2 months ago 15
Java Question

How to keep catching an exception until it's correct java

I'm writing a program that uses the Scanner class and want to use a try-catch block to catch InputMismatchExceptions. This is what I have written:

public class StudentInfo{

public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int creditHours = 0;
try
{
System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");
creditHours = scnr.nextInt();
}
catch(InputMismatchException e){
System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
Scanner input = new Scanner(System.in);
creditHours = input.nextInt();
}
String studentClass = checkStudent (creditHours);
System.out.println("Official Student Classification: " + studentClass);
}


The try-catch block works one time around as in, if I put in 24.5 for example the first time around, it catches the exception and has the user retype the number of credit hours they have but if they retype a non-integer a second time, it fails to catch the error again and send out the appropriate message. So essentially, I was wondering if there was any way that it could keep catching the exception and sending out the error message no matter how many times they try it. I've tried using a do-while loop or a while statement but it doesn't work so yeah. Also, I created a new scanner variable in the catch block because if not, it doesn't allow me to input a new integer after giving the error message for some reason. It literally will throw the error that I typed and then proceed to give me Java's InputMismatchException error.

Here is my attempts at using a while loop:

int creditHours = 0;
while(creditHours <= 0){
try
{
System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");
creditHours = scnr.nextInt();
}
catch(InputMismatchException e){
System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
Scanner input = new Scanner(System.in);
creditHours = input.nextInt();
}
}

String studentClass = checkStudent (creditHours);
System.out.println("Official Student Classification: " + studentClass);
}

Answer

you need the try-catch to be in a loop in order to re-run your code.

try this:

 public class StudentInfo{

 public static void main(String[] args){

    int creditHours = 0;
    boolean ok = false;
    System.out.println("Please enter the number of credit hours that you currently have (do not count credit hours from classes that you are currently attending this semester)");

    while (!ok)
    {
        try
         {
           Scanner scnr = new Scanner(System.in);
           creditHours = scnr.nextInt();
           ok = true;
         }
        catch(InputMismatchException e){
           System.out.println("CLASSIFICATION ERROR: NUMBER NOT RECOGNIZED. ENTER AN INTEGER FOR CREDIT HOURS");
        }
    }


    String studentClass = checkStudent (creditHours);
    System.out.println("Official Student Classification: " + studentClass);
    }