1

I am working on making a simple java program which receives inputs from the user in order to generate silver spot prices. I want to know if I am closing the program correctly...

I have an option within a while loop which allows the user to end the program by pressing "0." My goal is to clear the screen and exit the program, but I'm not sure if this is the best way to go about doing it:

else if (itemWeight == 0){
                    System.out.print('\u000C');                        
                    System.exit(0);
user3473001
  • 71
  • 1
  • 7

3 Answers3

1

System.exit(0) is the best way to do it as far as I know.

the-alexgator
  • 298
  • 1
  • 5
  • 11
1

You don't necessarily need to use System.exit() unless you really need to.

Another way is with a flag,

boolean running = true;
while (running) {
    // get input...

    if (itemWeight == 0) {
        running = false;
    } else {
        // ...
    }
}
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
  • You cannot use 'continue' as the name of your variable because it is a keyword. But every other identifier would work. – Clashsoft Jan 21 '15 at 17:59
0

Yes just use system.exit(), it's the cleanest way of doing it:

System.exit(0);

Here is a Tuto.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78