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

Thread: scan.nextLine doesn't work in a while loop.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default scan.nextLine doesn't work in a while loop.

    When I put scan.nextLine in a while loop, it becomes a space the second time I go through it. I want it to read a quote even if I have a space but the side effect is that it doesn't read in what the user types in the second time if it's in a while loop. Can someone tell me how to fix this problem?

    // *******************************************
    // Palindrome.java
    //
    // Author: Andrew Date: 10/21/12
    // *******************************************

    import java.util.Scanner;

    public class Palindrome
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    String again = ("Yes");

    while (again.equalsIgnoreCase("Yes"))
    {
    int x = 0;
    System.out.println("Please enter in a phrase.");
    String phrase = scan.nextLine();
    String phrase1 = phrase.toLowerCase();
    while ((x<(phrase1.length()/2))&&
    (phrase1.charAt(x)==
    phrase1.charAt(phrase1.length()-1-x)))
    {
    x++;
    }
    if (x>=(phrase1.length()/2))
    System.out.println("The phrase " + phrase
    + " is a palindrome.");
    else
    System.out.println("The phrase " + phrase +
    " is not a palindrome.");
    System.out.println("Please enter in Yes to " +
    "test another phrase. " +
    "Enter in No to quit the "
    + "palindrome test.");
    again = scan.next();
    }
    }
    }

    Welcome to DrJava.
    > java Palindrome
    Please enter in a phrase.
    [Stanley Yelnats]
    The phrase Stanley Yelnats is a palindrome.
    Please enter in Yes to test another phrase. Enter in No to quit the palindrome test.
    [Yes]
    Please enter in a phrase.
    The phrase is a palindrome.
    Please enter in Yes to test another phrase. Enter in No to quit the palindrome test.
    [DrJava Input Box]


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: scan.nextLine doesn't work in a while loop.

    It sounds like you may have a potential buffer problem. At the end of your while loop, try making phrase equal to a blank phrase. It will clear the buffer and and help avoid any unnecessary inputs.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: scan.nextLine doesn't work in a while loop.

    Quote Originally Posted by ATL1994 View Post
    When I put scan.nextLine in a while loop, it becomes a space the second time I go through it.
     
        while (again.equalsIgnoreCase("Yes"))
        { 
          int x = 0;
          System.out.println("Please enter in a phrase.");
          String phrase = scan.nextLine();.
    .
    .
     
          again = scan.next();
        }
    After you scan the "Yes" at the bottom of the loop, the line terminating character is still in the Scanner input stream. Then at the top of the loop, scan.nextLine() sees that and removes it from the input stream and returns an empty String.

    Solution: use scan.nextLine() at the bottom of the loop (instead of scan.next()) to read the user input and to "flush" the input stream so that the input stream will have to wait for more user input the top of the loop.


    Cheers!

    Z

Similar Threads

  1. [SOLVED] Why won't it do a simple scan.nextLine?
    By boxofsoap in forum Java SE APIs
    Replies: 1
    Last Post: November 19th, 2011, 02:59 PM
  2. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  3. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 03:20 PM
  4. name=scan.nextLine();
    By Amro in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 07:38 PM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM

Tags for this Thread