Question:
How to use System.exit(0) in Java?
?
2013-10-08 11:41:23 UTC
Hey, I'm having a problem in Java with System.exit(0). My code is this:


import java.io.*;
import java.util.*;
public class Prog175a
{
public static void main (String[] args)
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Enter a Number [zero to quit]"); //Enter 6
int value1 = kbReader.nextInt();
System.exit(0);
int x = 1;
for(int a = 1; a<=6; a++)
{
x = x*a;
}
System.out.println("The value of 6! is " + x);
System.out.println(" ");
System.out.print("Enter a Number [zero to quit]"); //Enter 9
int value2 = kbReader.nextInt();
System.exit(0)
int d = 1;
for(int c = 1; c<=9; c++)
{
d = d*c;
}
System.out.println("The value of 9! is " + d);
System.out.println(" ");
System.out.print("Enter a Number [zero to quit]"); //Enter 12
int value3 = kbReader.nextInt();
System.exit(0);
int e = 1;
for(int f = 1; f<=12; f++)
{
e = e*f;
}
System.out.println("The value of 12! is " + e);
System.out.println(" ");
System.out.print("Enter a Number [zero to quit]"); //Enter 14
int value4 = kbReader.nextInt();
System.exit(0);
long g = 1;
for(int h = 1; h<=14; h++)
{
g = g*h;
}
System.out.println("The value of 14! is " + g);
System.out.println(" ");
System.out.print("Enter a Number [zero to quit]"); //Enter 0
int value5 = kbReader.nextInt();
System.exit(0);
}
}


If I enter "0" into any of the places where prompted to put a number, then the program should quit. However, I don't know where to put this command. I tried placing it in different spots, but it never worked (The code above shows where I placed System.exit(0) when I first wrote the program). It either continues to run the program or terminates the program when I don't want it to terminate. Could someone please help me with this?

Thanks in advance.
Three answers:
brilliant_moves
2013-10-08 11:57:27 UTC
int value1 = kbReader.nextInt();

if (value1 == 0) System.exit(0);



similarly for value2 .. value5



Good luck!
green meklar
2013-10-09 00:08:46 UTC
You need to include some sort of conditional statement, for instance:



int value1 = kbReader.nextInt();

if(value1==0)

{

System.exit(0);

}



That said, using System.exit() is considered bad form to begin with. Your program should exit naturally once all its threads have reached an end, rather than suddenly bailing on a System.exit() call. It's kind of like the difference between turning off your computer using Start -> Shut Down vs turning off your computer by smashing its power cord with a sledgehammer; one of them is drastic and inelegant and should really be avoided except as a last resort.
anonymous
2016-03-13 02:02:29 UTC
Not sure what is going on in this thread, though I for one agree with Angel on this one. If you practice common sense, and have an up to date version of java with up to date security software., then you should be ok. I laugh sometimes when people are so worried about java and telling people to completely remove all traces of it (though you can IF you have no need for it, though many apps REQUIRE java to run at all), while running an unpatched (with many non-installed) version of say Windows Vista. MS itself (I am completely guessing... I do not have stats to back this up, I admit it) seems to have just as many security issues as java itself, but some seem to "freakout" over java issues (I do recommend updating any PC you have that does have java on it) also, if your in a corporate environment, I could understand completely disabling java, though since so many apps and web apps need it, mass uninstalling it wouldn't (just my opinion) be advisable until you know you will not end up removing it on a ton of computers, to find out, it is needed for some reason, THEN have to reinstall it back all over again. Testing it, by disabling it (say in Firefox Plugins) in your browser but not totally removing it, is a good idea , if you want to test, if sites need it. You can simply re-enabling it if you need to later -Xmetalfanx


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...