Kenny Huynh Kenny Huynh -4 years ago 206
Java Question

Java scanner not functioning as expected

I am having trouble getting my scanner to work. I have a menu option and whenever the user selects an option, it will do something and then prompt the user back to the menu. In the code below, I have a command called Create Event, where it will take in user inputs for title/date/startingtime/endingtime. Then it will create the event and add it to the events data structure, then prompt the user back to the main menu. However, after creating an event, it prompts me saying wrong action.

Here is my code:

Scanner in = new Scanner(;
boolean check = true;
String choice;
do {
"Select one of the following options. \n [L]oad [V]iew by [C]reate [G]o to [E]vent List [D]elete list [Q]uit");
choice = in.nextLine();
switch(choice) {
case "Q":
check = false;
case "L":
String test = in.nextLine();
case "C":
System.out.println("Enter title:");
String title = in.nextLine();
System.out.println("Enter date: Format should be: MM/DD/YYYY ex: Feb 12, 2015 would be 02/12/2015");
String date = in.nextLine();
System.out.println("Enter starting time: (24hr clock format) 12AM = 2400 12PM = 1200");
int startingTime = in.nextInt();
System.out.println("Enter starting time: (24hr clock format) 12AM = 2400 12PM = 1200");
int endingTime = in.nextInt();
Event event = new Event(title, date, startingTime, endingTime);
System.out.println("Event created.");
System.out.println("Wrong action");

} while(check);

After an event is created, it prompts me to the main menu then says I have entered a wrong action then prompts me to the menu again. I am assuming that it is reaching the default statement, though I didn't enter anything after I created my event. Any help would be appreciated. Thanks!

Answer Source

Make sure that every time you use


add one additional line of code


to read through the rest of the line so that the next input wouldn't be affected.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download