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.