I'm making a basic menu for what is meant to be a pet store, in the program I'm writing, you can own multiple pets and buy them from this store. I run this menu on the command line and attempt to get user input using scan.nextInt()
, what happens is the menu repeats a second time, but then typing anything in on the second loop seems to do nothing and the program proceeds. Internet searches suggested I need to clear the scanner using a scan.nextLine()
but that doesn't seem to fix it either. If you need more code tell me but this should be sufficient, I'm newer to programming so be gentle
public Pet showMenu()
{
Scanner scan = new Scanner(System.in);
int result = 10;
while(true)
{
System.out.println("Welcome to the pet store: ");
System.out.println("Please pick a pet ");
for (int i = 0; i < availablePets.size(); i++)
{
System.out.println(i + ")");
availablePets.get(i).printStats();
System.out.println();
}//End for
try
{
System.out.println("Which pet would you like? (Type any other number to quit)");
result = scan.nextInt();
scan.nextLine();
if (result >= 0 && result <= 3)
{
Pet tempPet = availablePets.get(result);
availablePets.remove(result);
return tempPet;
}
else
{
Pet quitObj = new Pet("Quit");
return quitObj;
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}