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 2 of 2

Thread: Get input from the user till he type quit

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Get input from the user till he type quit

    Hi,

    I'm trying to write little program that get input from the user (using BufferedReader and readLine() function) the program should run till the user type 'quit'.

    so i write something like this
    while(true)
    {
    System.out.println("Enter first number: (or type quit to exit program) ");
    if(br.readLine() == "quit")
    break;
    else
    ...
    }

    The problem is that br.readLine() == "quit" doesn't working...
    when I type quit or something else the program always go to the else...

    Where is my mistake?

    Thnks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Get input from the user till he type quit

    Your mistake - a common one - is in using the equals operator, '==', rather than the equals() method to compare Strings.

    Another construct to accomplish the same thing, perhaps simpler, is a do/while loop:
            do
            {
                // get user input
            }
            while( !input.equals( "quit" ) );

Similar Threads

  1. Replies: 1
    Last Post: July 1st, 2013, 08:36 AM
  2. User Input help
    By dx2731 in forum Java Theory & Questions
    Replies: 3
    Last Post: May 13th, 2013, 12:46 AM
  3. Ill keep asking till i get an answer
    By iamgonge in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2013, 08:58 AM
  4. User Input help
    By lanmonster in forum What's Wrong With My Code?
    Replies: 20
    Last Post: January 20th, 2013, 10:44 AM
  5. can Java create a user define type of classes?
    By LearningJava in forum Java Theory & Questions
    Replies: 5
    Last Post: September 30th, 2011, 10:49 AM