Reputation: 111
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?
Upvotes: 2
Views: 157
Reputation: 299208
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
}
Upvotes: 4
Reputation:
As many have already pointed out System.exit(0) is the way to terminate your application. So you have two arrays.
Pseudo code
loop thru array1
if(array1[i] != condition)
System.exit(0)
loop thru array2
if(array2[i] != condition)
System.exit(0)
Upvotes: 1
Reputation: 88801
if (condition) {
System.exit(0)
}
This will make your program exit when condition is true. exit
is a static method on java.lang.System
You can return other values than 0 to signal the status of the exit as appropriate.
Upvotes: 2
Reputation: 52247
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.
Upvotes: 3
Reputation: 129
Terminating a the running process in Java: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#exit(int)
You should be more specific about the condition question you have, it's not very clear now.
Upvotes: 0