This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

having a problem exiting out of my while loop

 
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?
 
author
Posts: 23855
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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...
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!