Results 1 to 2 of 2
  1. #1
    Stev0 is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default Class Scanner looping issue

    The following code works until the loop is made back to the item one where it skips (as if the enter key was hit) to item two. This seems like a Scanner class issue. Maybe I am not using it properly.

    ************************** code********************
    import java.util.Scanner;

    public class Loop
    {
    // main method begins execution of Java application
    public static void main( String args[] )
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in );

    String item1, item2;
    double n1, n2;

    while (true)
    {
    System.out.print( "enter item one or stop to exit " );
    item1 = input.nextLine();
    if (item1.equalsIgnoreCase ("stop"))
    {
    System.out.printf( "You entered 'stop'.\n");
    break;
    }
    System.out.printf( "enter item two " );
    item2 = input.nextLine();
    System.out.printf( "enter number one " );
    n1 = input.nextDouble();

    System.out.printf( "enter number two " ); // prompt for rate
    n2 = input.nextDouble();

    System.out.printf( "item one is %s\n", item1);
    System.out.printf( "item two is:%s\n", item2);
    System.out.printf( "number one is %.2f\n", n1);
    System.out.printf( "number two is %.2f\n", n2);
    }
    }
    }
    ************end code **********
    This version not using class Scanner works.
    ************code version two ***
    //import java.util.Scanner;
    import java.io.*; //allows for input and output streams from the java IO package
    import java.text.*;
    public class Loop2
    {
    public static void main( String args[] )throws Exception
    {
    DataInput keyboard = new DataInputStream(System.in);

    String item1, item2;
    double n1, n2;

    while (true)
    {
    System.out.print( "enter item one or stop to exit " );
    item1 = keyboard.readLine();
    //item1 = input.nextLine();
    if (item1.equalsIgnoreCase ("stop"))
    {
    System.out.printf( "You entered 'stop'.\n");
    break;
    }
    System.out.printf( "enter item two " );
    item2 = keyboard.readLine();
    //item2 = input.nextLine();
    //System.out.printf( "enter number one " );
    //n1 = input.nextDouble();

    //System.out.printf( "enter number two " ); // prompt for rate
    //n2 = input.nextDouble();

    System.out.printf( "item one is %s\n", item1);
    System.out.printf( "item two is:%s\n", item2);
    //System.out.printf( "number one is %.2f\n", n1);
    //System.out.printf( "number two is %.2f\n", n2);
    }
    }
    }
    **********end code version two *****
    Any thought on the Scanner class skipping item one?
    Thank you in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The nextLine method blocks execution. The other methods may not.
    Java Code:
    // So try replacing
    n1 = input.nextDouble();
    // with something like
    n1 = Double.parseDouble(input.nextLine());

Similar Threads

  1. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 01:41 AM
  2. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 10:11 AM
  3. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:34 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 08:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 07:55 PM

Posting Permissions

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