Results 1 to 3 of 3
  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default Basic Loops stalling with While Statement Mystery

    I am learning loops and wrote a very simple program to test my skills. In the first example, everything works fine. I added a second part to the loop in the second program and it no longer loops. I know this has to be easy but after hours of searching for an answer I decided to see if any of the experts out there could help. Thanks in advance for your help!

    Gary

    FIRST EXAMPLE - WORKS FINE

    import java.util.*;
    public class loopTest {

    public static void main(String[] args){

    String response;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Ready to start? Enter Y or N");
    response = keyboard.nextLine();

    while(response.equalsIgnoreCase("Y"))

    {
    System.out.println("in the loop");

    System.out.println("Do again? Enter Y or N");
    response = keyboard.nextLine();
    }

    System.out.println("out of loop");


    }

    }

    SECOND EXAMPLE STALLS!! I added a second instruction in the loop. When it gets to the end of the loop it doesn't wait for the user to enter a number, goes back to the WHILE statement, then ends. :mad:

    import java.util.*;
    public class loopTest {

    public static void main(String[] args){

    String response;
    int number;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Ready to start? Enter Y or N");
    response = keyboard.nextLine();

    while(response.equalsIgnoreCase("Y"))
    {
    System.out.println("in the loop");

    // New part to test loop
    System.out.println("Enter a number");
    number = keyboard.nextInt();
    System.out.println(number);


    System.out.println("Do again? Enter Y or N");
    response = keyboard.nextLine();
    }

    System.out.println("out of loop");


    }


    }

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Found this here -
    Be careful about nextLine. It reads the rest of the current line. If you are in the middle of a line, or even if you are just before the end, it only gets the rest of this line. For example, suppose that you expect the user to type an integer and a string. You might try to read them as follows.

    Scanner in = new Scanner(System.in);

    System.out.print("How many? ");
    int num = in.nextInt();

    System.out.print("What is your name? ");
    String name = in.nextLine();
    But that does not work. The problem is that the user typed a newline character after the integer. Expression in.nextInt( ) reads the integer, but no more. It leaves the end-of-line marker still in the input. Now in.nextLine( ) just reads the rest of the line that contains the integer, which is probably an empty string. A better way to get a number and a string is as follows.
    Scanner in = new Scanner(System.in);

    System.out.print("How many? ");
    int num = in.nextInt();
    in.nextLine(); // Skip the rest of this line.

    System.out.print("What is your name? ");
    String name = in.nextLine();

  3. #3
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default Thank you!

    After hours and hours of staring at this I got it to work in 30 seconds with your help. I totally appreciate it.

    Gary

Similar Threads

  1. basic while loops
    By ts96 in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 12:10 PM
  2. Derby Mystery..!!!!
    By yiantzi in forum JDBC
    Replies: 3
    Last Post: 01-24-2011, 11:59 AM
  3. Basic loops
    By Samari413 in forum New To Java
    Replies: 13
    Last Post: 11-14-2010, 01:19 AM
  4. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •