5 Replies Latest reply: Feb 24, 2012 9:54 PM by 796440 RSS

    System.exit

    918789
      Does anyone know of a trick that I can use to restart the same java application from the beginning, instead of just using "System.exit"?
        • 1. Re: System.exit
          796440
          You really should never need to use System.exit at all. As for restarting from the beginning, it depends on whether you want to keep the same JVM. Assuming you do, it would be something like this:
          // instead of this:
          public void main(String[] args) {
            doStuff();
          }
          
          
          // do this:
          public void main (String[] args) {
            while (some condition that I use to determine whether to keep going) {
              doStuff();
            }
          }
          • 2. Re: System.exit
            918789
            Thanks a lot for the info! The reason I ask is because I'm making a miles per gallon calculator for school, and we're supposed to make it so that if the user types in "0" for the miles they've travelled or the gallons they've put in the tank, then the program will exit. So I thought it'd be better if I can have it exit the program, then just start back at the beginning. If I'm to do like you said, what type of statement would you put after the "while statement" to make it true or false? Can I make it to run the "while loop" if my variable is greater than zero, but restart the program if my variable is 0?
            • 3. Re: System.exit
              796440
              915786 wrote:
              So I thought it'd be better if I can have it exit the program, then just start back at the beginning.
              Once you exit the program, it's no longer running, so any Java code that you have that says anything about "going back to the beginning" can't run, because the JVM has already exited.
              If I'm to do like you said, what type of statement would you put after the "while statement" to make it true or false? Can I make it to run the "while loop" if my variable is greater than zero, but restart the program if my variable is 0?
              I don't understand your question. Have you studied while loops yet? The basic idea is something like this:
              get user input
              while (that input is not 0) {
                do our work
                get user input
              }
              There are other ways to structure it, but that's a pretty common and simple one.
              • 4. Re: System.exit
                918789
                Ok, that answers my question perfectly. We have studied while loops a little bit, but I was thinking that I could do something like while(true) run the program, while(false) restart the program from the beginning. Basically, the part you said about exiting the JVM summed it up for me. Thanks for your help!
                • 5. Re: System.exit
                  796440
                  You're very welcome. Glad it was useful. :-)