• Post Reply Bookmark Topic Watch Topic
  • New Topic

having a problem exiting out of my while loop  RSS feed

 
June Rodriguez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using switch to allow the user to chose items from a menu and I have all items working except my quit. I have
boolean finished = false;
while (!finished)
{
int decision = getUserInt(userResponse);
switch (decision){
case 1: createANewFleet();
break;
case 2: addATruck();
break;
case 3: fillAllTrucks();
break;
case 4: displayFleetToString();
break;
case 5: finished = true;
break;
default: break;
}
printMenu();
userResponse = scan.nextLine();
}
System.out.println("This will end your current session. Goodbye!");
}

the problem is.. I have to press 5 twice in order to exit. Any suggestions?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
June Rodriguez wrote:I am using switch to allow the user to chose items from a menu and I have all items working except my quit. I have


the problem is.. I have to press 5 twice in order to exit. Any suggestions?


Basically, take a look at what happens when you enter 5. Follow the code, and you will see that the program asks for more input, even after the 5 has been entered.

Henry
 
June Rodriguez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for taking the time to respond

I do see that. The thing is, i have a "priming" print menu and then after the user makes a decision and the case breaks the menu has to appear again and the the user has to make another choice. The only time this is not necessary is when I am quitting the program. When I press 5 the second time the program terminates and the goodbye message prints. I'm just trying to find a way to jump to the beginning of the loop right after the boolean value is changed so that the program bypasses the while loop (since it should now be closed) and the ending message prints right away.
 
June Rodriguez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it! I just moved some things around...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!