• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to stop a program?  RSS feed

 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear,
I need my program to stop executing if a certain condition is true! e.g.:I want to check a condition IF the result is yes stop executing the program, ELSE do something.

forexample:


by "stop executing" I mean that I dont want the compiler to execute those lines after blocking/stopping(like println meassage)! but how can I do this?
(I already try with block(); but doesnt work)

Thank you so much,
Sahar.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats what your if-else is for: anything that you write in the else-branch won't be executed (instructions after the else-branch still will be done).

If you really want to end your program, you can use System.exit(). This will cause the java virtual machine to stop executing, so make sure you really do this as the last instruction (for example, after your block() method).
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you get block() from?
The compiler doesn't execute a program; the JVM does.
And I see D. Ogranos (thank you) has already given you some useful information.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friend,
Thank you sooo much for the reply!
1) the ELSE part will not help me because any way the rest of the code (those which are after the end of else) will be executed!!

2)I also cant use the system.exit because: there are some other classess which are running (or are to be runned later on) and If I use System.exit() they will be terminate as well.

All Im looking for, is to know a statment or what ever that can help me to stop my compiler from executing the rest of the code after that certain statement!
I even dont know If there exists such a statement??!!

Any suggesstion would be really appreciate!

Sahar.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are no statements that stop the execution of a program. "break" and "continue" statements end a loop iteration, but I don't think they will help you here (since you have no loop). Can't you arrange your code such that no other instructions follow on your test?
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friend,
unfortunately, I spend whole my day trying to do that, but it seems impossible as it follows by about 800 line!
Any Idea? or any keyword that I can search for which may help me?

Thank you before hand,
Sahar.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you have 800 lines in the same method? You can try a return statement, but you really want to refactor that method. A method that large is unlikely to have been written on object-oriented principles.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And an 800-line method is not a "beginning" topic. Moving thread.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"stop executing" to me means 'shut down the program' - i.e. everything stops. This is not what you want, apparently.

What exactly do you need to happen? does the stuff after the 'else' block need to run every time or not? if yes, leave it out of the else. if no, but it in the else.
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
Thank you ALL!
Dear fred : My bad! you are right! its not "stop executing". I just need the thread to stop when it reachs that command!(by stop I mean tht JVM dose not execute the line after that certain command!)
Dear Campbell: Im agree with you! 800 line is so much but I need to have all of them in that method. the only thing I can do is try to encapsulate them in to some other methods and just call them in my main method! It will be more organised and modular!
Can you please introduce me an online sorce or book that can help me to know how to partition my method to some other methods?(I mostly have problem with how to access those variables which are accessible in main method but are not in new methods)

Thank you guys again!
Sahar.

 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahar, how many classes are in your program?
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear,
I have a quiet big project. contain 3 package each contains 2 to 7 classess. some classess are about 200-300 lines and some nearly a thousand lines! (which seems are making problem for me)!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!