First, there's an issue in the do-while
loop in getSystemChoice
which exits only when user selects options 1 or 2.
Thus, the logic of this loop/method should be fixed:
public static int getSystemChoice() {
Scanner input = new Scanner(System.in); //create scanner
int systemChoice;
do {
System.out.println("If you are using the Metric system, please enter a 1.");
System.out.println("If you are using the Imperial system, please enter a 2.");
System.out.println("To quit the program, please enter a 3.");
systemChoice = input.nextInt();
// exit here if 3 is selected
if (systemChoice == 3) {
System.exit(0);
}
}
while (systemChoice != 1 && systemChoice != 2);
return systemChoice;
}
However, this method may still need improvement to avoid repeated printing of the same menu and print a message about invalid input instead.
Also, the existing code will throw an InputMismatchException
if a non-integer value is entered, so it may be better to use input.next()
to read any token from the input as a String.
public static int getSystemChoice() {
Scanner input = new Scanner(System.in); //create scanner
// print menu once
System.out.println("If you are using the Metric system, please enter a 1.");
System.out.println("If you are using the Imperial system, please enter a 2.");
System.out.println("To quit the program, please enter a 3.");
while (true) {
String systemChoice = input.next(); // read any token from the input as String
switch (systemChoice) {
case "1": case "2":
return Integer.parseInt(systemChoice);
case "3":
System.out.println("Bye!"); // optional message to indicate exit
System.exit(0);
default:
System.out.println("Invalid input, please enter 1, 2, or 3");
}
}
}