import java.util.InputMismatchException;
import java.util.Scanner;
class ClassB {
public static void main(String[]args) throws Exception{
ClassB b = new ClassB();
b.getInput();
}
public void getInput() throws Exception {
String label = null;
int input;
Scanner scan = new Scanner(System.in);
for (input = 1; input != 4; input++){
switch(input){
case 1: label = "name";
break;
case 2: label = "password";
break;
case 3: label = "Room number";
break;
}
System.out.println("Enter your " + label);
scan.next();
try{
if (input == 1){
int name = scan.nextInt();
//residence.changeName(name);
}
else if(input == 2){
String password = scan.next();
}
int rmNumber = scan.nextInt();
}catch (IllegalArgumentException | InputMismatchException me ){
String type = "A string";
String message = (input == 1 || input == 2) ? type : "An integer";
input = 1;
}
}
}
}
When I run this code, the first scan.next() in try-catch block does not respond when the enter key of the computer is pressed, and because of this, the subsequent if () statement cannot be entered. The cursor in the text field only breaks line but the input is never accepted. I am using netbean IDE