6 Replies - 2717 Views - Last Post: 30 August 2007 - 09:58 AM Rate Topic: -----

#1 Mastergeek666  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 10-August 07

How to end a program with code?

Post icon  Posted 29 August 2007 - 10:17 AM

Hey guys not trying to make you do homework I just need to know how to end my program if someone types no. Thanks in advance if you do choose to help.
Is This A Good Question/Topic? 0
  • +

Replies To: How to end a program with code?

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: How to end a program with code?

Posted 29 August 2007 - 10:25 AM

I am pretty much sure if you try to search this forum you'll be able to dig something up that can help you. Also if you have your idea, show us the code and tell what is the trouble that's stopping you to end the program the way you want.
Was This Post Helpful? 1
  • +
  • -

#3 Mastergeek666  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 10-August 07

Re: How to end a program with code?

Posted 29 August 2007 - 10:29 AM

View PostPennyBoki, on 29 Aug, 2007 - 10:25 AM, said:

I am pretty much sure if you try to search this forum you'll be able to dig something up that can help you. Also if you have your idea, show us the code and tell what is the trouble that's stopping you to end the program the way you want.



kk
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: How to end a program with code?

Posted 29 August 2007 - 10:35 AM

Are you familiar with input output procedures? Here is a link that goes through some common input output routines:

http://pages.cs.wisc.../io/JavaIO.html

If you place the user input into a String object, you can use the .equals() method to determine if it matches "no", then exit.
Was This Post Helpful? 0
  • +
  • -

#5 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: How to end a program with code?

Posted 29 August 2007 - 02:23 PM

Get the input string, then use equals() to find if it is "no", then call System.exit(0); to exit the program, and the trick is done :)

This post has been edited by alpha02: 29 August 2007 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: How to end a program with code?

Posted 29 August 2007 - 02:49 PM

It's not a nice habit to use System.exit(int) to end a program, since it "Terminates the currently running Java Virtual Machine."
This is generally not a big problem in simple programs, that only do things in memory. But if you deal with files or open sockets, some data can be lost and if you have more threads (even if the other one is a Swing thread), all of them are stopped at once, and without the proper cleaning up code your application can leave quite a mess behind itself, so a restart might be really painful.
So how to exit an application? From console application it is possible to simply return all the way back to the main function, and return from there and it is done. From multithreaded applications it is a bit more complex, but generally you are supposed to signal all threads to stop, and when all of them dead died peacfully (after cleaning up behind themselves) the VM will terminate itself. In case of Swing applications I recall something like that the VM might terminate after the main frame is disposed, but I am not sure about this, and what would happens if an active thread was still running. I am sure that either the Swing tutorial or the API itself has some details on it.
Was This Post Helpful? 2

#7 Mastergeek666  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 10-August 07

Re: How to end a program with code?

Posted 30 August 2007 - 09:58 AM

View Postalpha02, on 29 Aug, 2007 - 02:23 PM, said:

Get the input string, then use equals() to find if it is "no", then call System.exit(0); to exit the program, and the trick is done :)



Thx for the help!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1