aspose file tools*
The moose likes Beginning Java and the fly likes line of exit program code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "line of exit program code?" Watch "line of exit program code?" New topic
Author

line of exit program code?

Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
Ya. as the subject says, i would like to know of a line of code i could use that would exit the program.
the purpose of this is to use it in a for-loop.
thanks for any help you can give
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
You can exit from the for-loop using a break statement. Read break.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Check out System#exit()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
thanks for the responses
sorry John but thats leaving the for-loop process.
i am looking for a line of code used to exit the actual program.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes - you are right. I would prefer a normal program execution but not sure of your requirements.
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
Actually maybe it would be better to find a line of code that will start the program again.
im sort of just messing with loops to get better. i am making a game that sort of resembles zork.
of course it will be much much shorter.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If you want to just leave out a iteration in a for loop, you can use continue. You have to tell the details and post some code to help you better.
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
I have no idea about the story and am making it up as i go. so i dont think any comments are needed about that.
So when i run this no matter what i type in it results in the first section of the if statement.
The reason for the exit or restart code is so if the character dies it will print you died and will restart or end.

WARNING some text is gory!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I would advise you to let the for loop terminate by reaching its end. If you count it correctly, it will terminate when it finishes its count. I suggest you don’t try to terminate the JVM. When you reach the last line of the main method, and that completes, program control will return to the JVM which will turn off cleanly. System.exit() is a bit vicious. You won’t notice any problems at this stage, but if you ever use it in a multi‑threaded program, it might terminate a thread which hasn’t finished its task, and you can get corrupted files, etc., if there is a task unfinished.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
So when i run this no matter what i type in it results in the first section of the if statement.

The reason is because you use != for String non-equality. You should use the equals() method in the String class.
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
ok campbell. but do you have any suggestions on how to get the program to the end of the program?

What is the equals() syntax? how do i include it?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Bryce Wade wrote: . . . WARNING some text is gory! . . .
You are right. Those very long lines, which make the code difficult to read. That can be sorted out by closing the Strings with quotes " and using the + operator. You can see how it is done now I have changed it.
That use of \n which you see in so many books, but is mistaken. You are using the wrong line end for Windows®. You ought to use printf and the %n tag.
That use of the == operator or != on reference types, which usually gives you the wrong answer. You should use the equals method or this to compare those Strings.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You will see that the version without the ! is much easier to read and understand, so you should use versions without ! as far as possible
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
If you are using a Scanner, you can use its nextInt() method, then you get 1 2 3 or 4 as ints, and == does work on ints.
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
so is %n equal to \n ?
and thanks for all of them posts
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

As far as I know %n only works with System.out.printf (and String.format), not with System.out.println.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bryce Wade
Greenhorn

Joined: Feb 23, 2012
Posts: 17
and what is the difference between the two print statements?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
It uses \r\n on Windows® or \r on old Macs (probably not many of those around still) instead of \n which is correct on Linux. A lot of applications can compensate for wrong line ends, but some, eg Microsoft NotePad®, can’t.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Jesper de Jong wrote:As far as I know %n only works with System.out.printf (and String.format), not with System.out.println.
I said printf.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Bryce Wade wrote:and what is the difference between the two print statements?


What do their docs say?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Bryce Wade wrote:ok campbell. but do you have any suggestions on how to get the program to the end of the program?

I think you've already been told more than once: System.exit(int) - however, it's usually not needed in normal use.
How about something like this (in my bad pseudo-code):HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
Winston Gutkowski wrote:
I think you've already been told more than once: System.exit(int) - however, it's usually not needed in normal use.


Well, it is if you want to return a non-zero return code from your program.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Ivan Jozsef Balazs wrote:Well, it is if you want to return a non-zero return code from your program.

Hmmm. I prefer Exceptions myself, unless the return code is used simply as a form of IPC.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
There are very few things nowadays which actually use the exit value.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Bryce Wade wrote:thanks for the responses
sorry John but thats leaving the for-loop process.
i am looking for a line of code used to exit the actual program.


Why don't you create a unchecked exception in a if statement, in the for loop on the line where you want to exit from the actual program.
I am a newbie so please suggest me if i am posting something wrong.


OCPJP 6 86%
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
Campbell Ritchie wrote:There are very few things nowadays which actually use the exit value.


Every time I write a Java batch program, that is, one to be run from a script, I make sure the program exits with a useful return code.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Ivan Jozsef Balazs wrote:Every time I write a Java batch program, that is, one to be run from a script, I make sure the program exits with a useful return code.

¿Qué,...java batch? ¿Oxymoron, no?

I jest, of course; but the idea of running programs from scripts that can actually do something about a return code other than 0 (except to log it) seems a bit old-fashioned (and I'm an old Sysadmin, so I love scripts).

But please let me know if I'm wrong. It wouldn't be the first time.

Winston

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
Winston Gutkowski wrote:
but the idea of running programs from scripts that can actually do something about a return code other than 0 (except to log it) seems a bit old-fashioned


The script can make a decision depending on the return code of the Java program for example whether to continue or not the procedure.
With a potent scripting language (like bash), it is no problem to cruft together a script implementing some non-trivial piece of logic,
and a Java program can be a welcome component.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Ivan Jozsef Balazs wrote:With a potent scripting language (like bash), it is no problem to cruft together a script implementing some non-trivial piece of logic...

As I say, I'm an old Unix Sysadmin, so I love bash; but I'd be leery of any system containing complex logic that doesn't have the backing of a proper language (believe me, I've spent half my admin life documenting the scripts I've written).

Power != Efficacy

Winston
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
There might be a gap between "non-trivial" and complex.

A return-code is useful (beyond being logged) even in a non-trivial (albeit not complex) script.

Cheers!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ivan Jozsef Balazs wrote:
Winston Gutkowski wrote:
but the idea of running programs from scripts that can actually do something about a return code other than 0 (except to log it) seems a bit old-fashioned


The script can make a decision depending on the return code of the Java program for example whether to continue or not the procedure.
With a potent scripting language (like bash), it is no problem to cruft together a script implementing some non-trivial piece of logic,
and a Java program can be a welcome component.



I honestly can't remember the last time I wrote a shell script that cared about anything other than zero vs. non-zero. And I'm pretty sure that if the JVM dies due to an uncaught exception percolating out of main(), the result code will be non-zero, so I can't say I've ever come across a case where specific System.exit(N) values would be useful. Not saying there can't be such a case, just that, from the script's perspective, "JVM died due to NPE" vs. "JVM died due to missing input file" doesn't seem particularly useful.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
Jeff Verdegan wrote:
I honestly can't remember the last time I wrote a shell script that cared about anything other than zero vs. non-zero.


This does not contradict my humble opinion.

A Java program can deliberately (that is, if the JVM did not crash) return a non-zero code if it was not able to perform the task it was supposed to.
The distinction zero-non-zero is also a useful one.
The possibility to set the return code of a Java program is a Good Thing.

For example grep (known to be a highly useful utility also in scripts) returns 0 if the stuff is found, 1 otherwise but 2 on error (file not found etc.)

Lest we should want to write a grep clone in Java (jrep)...

http://www.opendap.org/api/javaDocs/gnu/regexp/util/Grep.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: line of exit program code?