Results 1 to 2 of 2
Thread: Class Scanner looping issue
- 05-25-2008, 03:41 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
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.
- 05-25-2008, 06:53 PM #2
Similar Threads
-
Using Scanner class to read int value
By Java Tip in forum Java TipReplies: 1Last Post: 02-07-2009, 02:47 AM -
Scanner class question
By Rgfirefly24 in forum New To JavaReplies: 5Last Post: 04-25-2008, 12:41 AM -
Getting tokens using Scanner class
By Java Tip in forum Java TipReplies: 0Last Post: 02-05-2008, 09:11 AM -
Scanner class
By ajaymenon.k in forum Advanced JavaReplies: 1Last Post: 11-26-2007, 07:01 AM -
JDK 5.0 Scanner Class
By Sircedric88 in forum New To JavaReplies: 3Last Post: 07-27-2007, 06:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks