• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to add a prompt to ask the user if they want to run the program again?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I was wondering how to add a prompt to ask the user if they want to run the program again. And then continue prompting for input, computing and displaying results until the user says that they do not want to run the program again. Apparently this can be done however I want, but I must use a String to signal the end of the program.

So far my program looks like the following:




I'm not sure what or where to add to prompt the user again, I believe I am to use another while loop?... All I know, is if they don't want to continue then I can end program System.exit (0);
Any insight would be greatly appreciated.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can wrap your code in a while loop, if the user might not want to answer the questions at all, or in a do/while loop, if the user needs to answer the questions at least once. Prompt for "end" to end the program, then test the input for "end" in the while part. Then just let the program "fall off the end." No reason to use System.exit();
 
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't. If you start adding more loops you will make that code into a right mess. You need separate methods rather than trying to squeeze a lot of code into the main method. It is likely that the first method will have a loop which calls another method and then asks for input as to whether you want to run again. That method would therefore contain about 4 or 5 lines of code.

Remember there is a kind of loop which always runs once and then considers whether to run again.

Why are you calling nextLine in line 25? That is only necessary if followed by another nextLine call.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To take Campbell's advice a little further...

Imagine if you had a method you called that did just about everything. I.e. your main looked something like:

public static void main(String[] args){
runProgram(); //this may need to take some arguments...
}


now answering your question is easy. you write a loop that calls that single method and asks if they want to run it again. You break out of your loop depending on what they input.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic