I'm making simple program where the user inputs a class number of a student(cn) and the grade of that student(ngrade). I was trying to add an exception that checks if the input of the cn and ngrade are integers. If not the user will be informed that it is invalid and asks to re enter the details.
problem: i did use while loop to check the inputs throughout the loop. but i'm getting a infinite loop.
CODE:
import java.io.*;
import java.util.*;
public class trrying {
public static void main(String[] args) {
int ngrade;
int cn;
int A = 0, B = 0, C = 0, D = 0, E = 0;
boolean test = false;
Scanner sn = new Scanner(System.in);
while (!test) {
try {
for (int i = 0; i <= 2; i++) {
System.out.println("Enter class number: ");
cn = sn.nextInt();
System.out.println("Enter numeric grade: ");
ngrade = sn.nextInt();
System.out.println("Letter Grade: ");
if (ngrade >= 90) {
A++;
System.out.println("A");
} else {
System.out.println("HELLO");
}
test = true;
}
test = false;
} catch (Exception e) {
System.out.println("ERROR! ");
// System.out.println("Enter class number: ");
}
}
}
}