Hey this is a small part of my program that I'm having big issues with. I want the program to give the user an error message and start the loop from the beginning if the input is not an integer, while I want it to continue into the switch/case if it is an integer. This is what I got so far, but it is not working since I get the inputmismatchexception if its incorrect and it doesn't continue in to the switch/case if it is correct?
public static void main(String[] args) {
ArrayList<Dog> doglist = new ArrayList<Dog>();
Scanner myscan = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\n************************************");
System.out.println("\nWelcome to the kennel club!");
System.out.println("\n************************************");
System.out.println("\n[1] Register new dog");
System.out.println("[2] Print out list");
System.out.println("[3] Increase age");
System.out.println("[4] Remove dog");
System.out.println("[5] Quit program");
System.out.println("\n************************************");
System.out.println("\nChoose: ");
int option = 0;
boolean inputOk = false;
do {
try {
option = myscan.nextInt();
inputOk = true;
} catch (InputMismatchException e) {
System.out.println("Option must be a number");
myscan.nextLine(); // to consume the \n that remains at the end of the line after using nextInt();
}
}
while (!inputOk);
switch (option) {
case 1:
System.out.println("Write name:");
String name = myscan.next();
System.out.println("Write race:");
String race = myscan.next();
System.out.println("Age:");
int age = myscan.nextInt();
System.out.println("Weight:");
double weight = myscan.nextDouble();
Dog dog = new Dog(name, race, age, weight);
doglist.add(dog);
break;
case 2:
System.out.println("Minimum length of tail:");
double userInput1 = myscan.nextDouble();
for (Dog d : doglist) {
if (d.getTailLength() >= userInput1) {
System.out.println(d.toString());
}
}
break;
case 3:
System.out.println("Name of dog:");
String userInput2 = myscan.next();
int flag = 0;
for (Dog d : doglist) {
if (d.getName().equals(userInput2)) {
d.increaseAge();
d.increasetailLength();
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("Error, can't find dog with name:" + userInput2);
}
break;
case 4:
System.out.println("Name of dog:");
String userInput3 = myscan.next();
Dog dogToRemove = null;
for (Dog d : doglist) {
if (d.getName().equals(userInput3)) {
dogToRemove = d;
System.out.println("Dog is removed");
}
}
if (dogToRemove == null) {
System.out.println("Error, can't find dog with name: " + userInput3);
} else {
doglist.remove(dogToRemove);
}
break;
case 5:
running = false;//Avslutar loopen och därmed programmet
System.out.println("Program finshed");
break;
default:
System.out.println("Error, choose between [1] [2] [3] [4] [5]");//Felmeddelande om valet är någon annan siffra än de som menyn innehåller
break;
}
}
}
You can add a do-while to read the option catching the InputMismatchException:
int option = 0;
boolean inputOk = false;
do{
try {
option = myscan.nextInt();
inputOk = true;
} catch (InputMismatchException e) {
System.out.println("Error, this is not an integer.");
myscan.nextLine(); // to consume the \n that remains at the end of the line after using nextInt();
}
}while (!inputOk);
This way it will loop until you get a valid number.
EDIT
If you want to print the menu again then you can do this: in your original code, change the break statement with a continue statement and also include a call to the nextLine
method to consume the remaining \n:
int option = 0; // don't read the value directly before validating that is actually an integer value or you will get the InputMismatchException
if (!myscan.hasNextInt()) {
System.out.println("Error, this is not an integer:");
myscan.nextLine(); // consume the \n
continue; // loop again
} else {
option = myscan.nextInt();
}