Hannah Short
Hannah Short

Reputation: 1

Program isn't acknowledging an input statement?

I'm attempting this for my final project. For some reason, my program isn't recognizing that I'm prompting the user for input, and instead of giving a space for the user to enter information, the program just quits. Here's the code:

int choice;
Scanner input = new Scanner (System.in);
System.out.println("Enter a number between 1 and 4.");
choice = input.nextInt(); 
if (choice > 0 && choice < 5) {
     System.out.print("To return to the main menu, enter 'main'. To return to the Forces Menu, enter 'forces'. To quit, enter 'quit'.");
     choice2 = input.nextLine();
     if (choice2.compareToIgnoreCase(MM) == 0) {
         mainMenu();
     } else if (choice2.compareToIgnoreCase(F) == 0) {
         forces();
     } else if (choice2.compareToIgnoreCase(X) == 0) {
         System.out.println("Thank you! Goodbye!"); 
     }   
}

The MM, F, and X, are just String variables to be used when the user inputs their second choice. After it says "To return to the main menu...enter 'quit'.", the program stops instead of prompting the user for input. If I replace the if(choice...) with a while(choice...) it just repeats the statement prompting whether or not to quit or return. I read a similar post where it suggested replacing the first input.nextInt(); with an input.nextLine();, but that didn't work for me. I'm programming in Netbeans 7.2, by the way.

Upvotes: 0

Views: 73

Answers (1)

someone
someone

Reputation: 6572

This the line you required

choice = Integer.parseInt(input.nextLine()); 

instead of choice = input.nextInt();

Or put

input.nextLine(); 

after your

choice = input.nextInt();

Upvotes: 1

Related Questions