I was doing my assignment and paycode is one of the variable that let user to key in. So this paycode i declare it as int. So what if the user type other than integer such as a,b,c,d or 1.5,2.5 and so forth. I try the if else function but when user input other than integer there will be error in it. And I wan to make it loop also when user key in paycode other than the integer and let user key in again. This is all I have now and i can't make it loop. Instead, it will turn into infinity loop.
int paycode;
boolean good = true;
do {
try {
System.out.printf("Key in the paycode of the worker according to the worker type: \n");
paycode = myscanner.nextInt();
if (paycode == 4 || paycode == 3 || paycode == 2 || paycode == 1) {
good = false;
}
else {
//user key in out of the range of pay code
System.out.printf("Paycode must be 1,2,3,4 \n");
}
}
catch(InputMismatchException e) {
System.out.printf("Paycode cannot have decimal places. \n");
System.out.printf("Key in the paycode of the worker according to the worker type: \n");
}
}
while (good);
JavaScript != Java