2

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?

2
  • Can you clarify what exactly you mean by "a Scanner that lets me introduce the elements of an array" Commented Aug 24, 2011 at 10:40
  • I think they're asking how to use java.util.Scanner to turn some kind of input into an array of values. Commented Aug 24, 2011 at 10:58

5 Answers 5

4

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
}
Sign up to request clarification or add additional context in comments.

8 Comments

@awoodland yes, and of course in case of System.exit(0) also, but this seems to be homework, and I think it's just about conditional statements.
a return in/from main will only terminate the main Thread. There still can be other Threads running... Example: the GUI starts some Threads if you have a visible JFrame and the application will still be running despite main having been terminated.
It may also terminate for unhandled exceptions too. If it's nested 10 calls deep in some other calls then simply returning is probably an awful idea too.
@awoodland true, what all of us are missing is context. I was assuming a basic programming task in a main class. No threads, no GUI. Of course you need to explicitly shut down a GUI, but you shouldn't do it with System.exit(0)
I have two problems with that view. 1) How do you signal non-zero exit when you really want it? and 2) It seems like an extension of "every method should have only one return statement at the end" to the whole application, whilst your answer doesn't seem to agree with the single method variant.
|
3

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.

1 Comment

True, but that's an awful idea.
2
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.

5 Comments

True, but that's an awful idea.
It's not always a bad idea, it depends upon the context which is lacking in this question hence a straight forward answer without second guessing the question.
I don't agree either that it's an awful idea. Think of it like a very small console app that should terminate if the user types exit.
And if you're writing a KISS Unix philosophy style tool it's exactly what you want.
@Farmor a Java program should terminate itself, not the VM running it. If your program can't be rewritten to use a simple return instead of System.exit(0) then I'm afraid you have extremely poor code design.
1

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)

Comments

0

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.

1 Comment

True, but that's an awful idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.