Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to make a program quit?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to make a program quit?

    Hello All, I am brand new the forums and have been working with Java for a few months now.
    I have a few questions:

    1. How would I go about making a statement that will quit when the user enters quit?
    I am asking the user to input some data but want the program to stop when the user inputs quit.

    2. Not sure if this is just more of a preference or something that should be done but I am wondering if when creating variables should they be initialized to something such as a 0 or " "?

    example:
    int number = 0;
    String name = "";

    Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: How to make a program quit?

    Quote Originally Posted by copelandtml View Post
    How would I go about making a statement that will quit when the user enters quit?
    I am asking the user to input some data but want the program to stop when the user inputs quit.
    And what part of this are you having trouble with? Getting user input? Doing different things based on something else? Exiting?

    Normally I'd tell you to investigate the System.exit() method, but I think something much simpler, like a nice structured if or case statement, would be much more appropriate.

    Quote Originally Posted by copelandtml View Post
    Not sure if this is just more of a preference or something that should be done but I am wondering if when creating variables should they be initialized to something such as a 0 or " "?

    example:
    int number = 0;
    String name = "";

    Thanks!
    That depends entirely on the context. Does the variable have a logical default value? Also, note that class variables already do have default values, even if you don't specify one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program quit?

    if u make a jbutton then put this in side of it
    System.exit(0);
    for example;
    private class ExitButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program quit?

    I'm having trouble with the exiting when the user enters quit.

    if(userInput == quit)
    {
    quit program? not sure how I would construct the if statement
    }

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: How to make a program quit?

    if(userInput == quit)

    needs to be

    if(userInput.equals("quit"))


    If you want to keep the loop alive whilst the user enteres values, I suggest a 'while' loop.
    Then inside that you can add the 'if' statement.

    For example:

    boolean on = true
     
    while(on){
    // prompt for user input
    if(userInput.equals("quit")){
    on = false;
    }
    //
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: How to make a program quit?

    Quote Originally Posted by JavaPF View Post
    if(userInput == quit)
    needs to be

    if(userInput.equals("quit"))
    That's assuming that userInput is a String. It could also be that the user is using ints or something of the like (enter 0 to quit).

    OP- What I meant was, do it the other way. Instead of checking for user input to quit, check for everything else and let the quit fall through:

    while(userInput != quit){
       if(userInput == optionA){
          //do option A
       }
       else if(userInput == optionB){
          //do option B
       }
    }
    //when you reach here, the user has chosen to quit
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program quit?

    OK, what about
    	while ((!endResponse.equalsIgnoreCase("Yes")) & (!endResponse.equalsIgnoreCase("nO"))){
    			System.out.print("Sorry did not understand your response, try that again.");
    			System.out.print("Rembember enter \"Yes\" or \"No\": " );
    			endResponse = keyboard.next();}
    	if (endResponse.equalsIgnoreCase("no"))
    		break;

    Also can you do.
    if (userInput.equalsIgnoreCase(trim("yes"));  //?

Similar Threads

  1. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  2. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM
  3. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 3rd, 2010, 12:33 AM
  4. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM