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: Continuously get user's input until an empty string is entered

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Continuously get user's input until an empty string is entered

    Again, I seem to find myself in a web of simple but difficult to figure out what the problem is. I have the code below that just keeps getting the user's name and displaying it until the user enter's an empty string. Well, to simulate that, I just hit the keyboard instead of entering any name but for some reasons I am not seeing in my code, the programme just keeps looping. Any help on this?

     
                    System.out.println("Enter your name : \n");
    		Scanner st = new Scanner(System.in);
    		while(st.hasNext()){
    			System.out.println("Enter your name : \n");
    			String name = st.nextLine();
                            System.out.println(name);
                            if(name==" ") break;
    			}
             System.out.println("you are out of the while loop now!!");


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    898
    Thanks
    0
    Thanked 166 Times in 148 Posts

    Default Re: Continuously get user's input until an empty string is entered

    Dont compare strings with "==", you HAVE to use the equals() method to compare Strings.

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Continuously get user's input until an empty string is entered

    Hi, even when I do the comparison :

    if(name.equals(" ")) break;

    It still doesn't get out of the loop.
    Last edited by help_desk; July 26th, 2014 at 12:20 PM. Reason: Edited the format

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Continuously get user's input until an empty string is entered

    That's not an empty String, that's a String with a space in it.
    Do:
    if(name.trim().equals(""))
    name.trim() will remove any leading or trailing spaces, and "" is an empty String. So this will evaluate to true if the String is empty or just a space.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    898
    Thanks
    0
    Thanked 166 Times in 148 Posts

    Default Re: Continuously get user's input until an empty string is entered

    Did you enter a single space character or an empty string? Because these two are not the same:
    "".equals(" ") == false

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Continuously get user's input until an empty string is entered

    When I tried this..the programme still keeps running in the loop for a while..before breaking out of the loop. How can I modify the code in such a way that immediately I enter "" in the input, the programme exits the while loop and goes to the next statement:
    System.out.println("you are out of the while loop now!!");


     
                   System.out.println("Enter your name : \n");
    		Scanner st = new Scanner(System.in);
    		while(st.hasNext()){
    			System.out.println("Enter your name : \n");
    			String name = st.nextLine();
                            System.out.println(name);
                            if(name.trim().equals("")) break;
    			}
             System.out.println("you are out of the while loop now!!");
    Last edited by help_desk; July 26th, 2014 at 02:25 PM. Reason: format line

  7. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Continuously get user's input until an empty string is entered

    Try putting the "if" expression at the top of the loop so it
    is executed/checked against before any of the loop code is
    executed. Also, place the expression in braces so it belongs
    in it's own field of scope.

    If none of these work then you are going to have to re-write your
    continuation condition of the said loop - so it will only execute when
    valid input is entered before the loop is reached.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  8. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    help_desk (July 27th, 2014)

Similar Threads

  1. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 11:26 PM
  2. Placing user input to a array and to then display it as a string
    By LaliB in forum Collections and Generics
    Replies: 5
    Last Post: January 12th, 2012, 11:41 AM
  3. Parsing a string to an int while receiving user input?
    By happyxcrab in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2011, 06:38 PM
  4. Parsing many Ints from a user input String.
    By helpmeplease111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2011, 09:41 PM
  5. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 02:48 PM

Tags for this Thread