I have a Java program that requires me to create two arrays and a condition.
In that condition I have to decide if the program should continue running or not.The problem is that I don't know how to stop the program from running if the condition is not respected.
Can anyone tell me how and also how can I create a Scanner that lets me introduce the elements of an array?
A simple (mono threaded) Java Program will run until it has no more statements to execute or until it encounters a return statement in the main method.
Make sure, when your condition is false, to either a) just return, or b) to have your actual code in an if or else block.
a)
public static void main(String[] args){
if(someCheck){
return;
}
// actual code here
}
b)
public static void main(String[] args){
if(someCheck){
// actual code in here
}
// program ends here
}
You can terminate the Java Virtual Machine like that:
System.exit(0);
See the documentation http://download.oracle.com/javase/1,5.0/docs/api/java/lang/System.html#exit(int)
Here you can also find a short explanation how a Scanner works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With