I've been working on a project that repeatedly asks for user input using commands until the user enters "quit", then uses a switch statement to run the corresponding function and I'm getting this issue where the invalid command error message pops up when it is not supposed to, any and all help is appreciated. I have condensed the code down as far as I can to reproduce the issue
import java.util.Scanner;
public class Main {
private static Scanner usrInput;
static void checkCommand(String command) {
switch (command) {
case "add" -> addPlayer();
case "quit" -> System.out.println("");
default -> {
System.out.println("Invalid command, please try again");
}
}
}
static void enterCommand() {
String input;
do {
System.out.print(">>> ");
input = usrInput.nextLine();
checkCommand(input);
} while (!input.equals("quit"));
}
static void addPlayer() {
System.out.print("Enter name:\n >>> ");
String name = usrInput.nextLine();
System.out.print("How old are you " + name + "\n >>> ");
int age = usrInput.nextInt();
System.out.println("Name: " + name + "\nAge: " + age);
}
public static void main(String[] args) {
usrInput = new Scanner(System.in);
System.out.println("Enter command(add):");
enterCommand();
}
}
The program should only print out that error message when an incorrect command is entered, I am pretty sure the issue is something with the usrInput.nextInt() for the age variable but I don't know what to do from here