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

Thread: Simple while loop exercise but problem with stopping when empty line is typed.

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Simple while loop exercise but problem with stopping when empty line is typed

    Hi again, I'm sitting and learning about while loops and stuck at the first exercise it which says:

    1.Write a sentinel lopp that repeatedly prompts the user for names. Once an empty line is typed, all names typed are displayed. For example:

    Type a person's name (or an empty line to stop): Marty
    Type a person's name (or an empty line to stop): Andrea
    Type a person's name (or an empty line to stop): Stuart
    Type a person's name (or an empty line to stop):
    Welcome to all: Marty Andrea Stuart


    The words written with bold is the user input. I've somehow gotten my code to work with adjustments.

    import java.util.Scanner;
     
    public class UserNames {
    	public static void main(String[] args0){
    		Scanner console = new Scanner(System.in);
     
    		String users = "";
    		String temp = "";
    		System.out.println("Type a person's name (or an empty line to stop): ");
    		temp = console.next();
     
    		while(!temp.equals("V")){
     
    			users += temp+" ";
    			System.out.println("Type a person's name (or an empty line to stop):");
    			temp = console.next();
     
    		}		
    		System.out.println("Welcome to all:"+users);
    	}	
    }

    As you notice fast it's when you press V that the program stops. I'm wondering how I can make it so that it fits the description in the exercise with empty line.
    In Eclipse I can't press space and then enter or just enter when the program is running. The cursor just moves to the next line and awaits input from the user who seem to be forced to type something except space or just enter.

    Anyone got suggestions or know what I'm doing wrong? Thanks
    Last edited by JavaN00b; May 17th, 2011 at 05:24 AM.


  2. #2
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Simple while loop exercise but problem with stopping when empty line is typed.

    What you want to do is to check if the string is empty (i.e. it's length is equal to zero).
    Check the following link, if you check the "Method Summary" table you will most likely find a method or two that you can use

    String (Java Platform SE 6)

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Simple while loop exercise but problem with stopping when empty line is typed.

    Scanner methods are tricky. Some require something be entered and will skip over white space and the Enter key waiting for input.

    Try different Scanner methods for reading the input. Perhaps nextLine() will work better.

  4. #4
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Simple while loop exercise but problem with stopping when empty line is typed.

    I would alter the condition of the while loop for this:

    while (the string is not empty)

    in combination with a different scanner method...
    Last edited by vanDarg; May 14th, 2011 at 06:24 PM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thumbs up [SOLVED]Re: Simple while loop exercise but problem with stopping when emp

    Quote Originally Posted by Norm View Post
    Scanner methods are tricky. Some require something be entered and will skip over white space and the Enter key waiting for input.

    Try different Scanner methods for reading the input. Perhaps nextLine() will work better.
    I won't swear on it but I'm quite sure I went through all of the scanner and string commands yesterday to find something that would work and accept space or enter as input. After hours of trying it didn't work out, I gave up and put it up here for help.....

    Anyway,
    import java.util.Scanner;
     
    public class UserNames {
    	public static void main(String[] args0){
    		Scanner console = new Scanner(System.in);
     
    		String users = "";
    		String temp = "";
    		System.out.println("Type a person's name (or an empty line to stop): ");
    		temp = console.nextLine();
     
    		while(!temp.equals("")){
     
    			users += temp+" ";
    			System.out.println("Type a person's name (or an empty line to stop):");
    			temp = console.nextLine();
     
    		}
     
    		System.out.println("Welcome to all:"+users);
     
    	}
     
    }

    Thanks a lot, using nextLine() did the trick. I don't know what I did wrong yesterday for it not to work. It works perfect now.^^
    I'm just surprised that it accepts as "Enter" as imput now when it wouldn't yesterday.

    Thanks again for your help people, I shall jump into the depths of coding once again^^

Similar Threads

  1. Starting and Stopping a loop with JButtons
    By Endevor in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 8th, 2010, 03:37 PM
  2. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 06:40 PM
  3. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 07:15 PM
  4. [SOLVED] ArrayOutofBoundary in Simple While Loop
    By Johnpower in forum Loops & Control Statements
    Replies: 6
    Last Post: June 13th, 2010, 05:31 AM
  5. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 03:06 AM