Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-25-2008, 05:41 PM
Member
 
Join Date: Apr 2008
Posts: 2
Stev0 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-25-2008, 08:53 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The nextLine method blocks execution. The other methods may not.
Code:
// So try replacing n1 = input.nextDouble(); // with something like n1 = Double.parseDouble(input.nextLine());
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scanner class question Rgfirefly24 New To Java 5 04-25-2008 02:41 AM
Getting tokens using Scanner class Java Tip Java Tips 0 02-05-2008 11:11 AM
Using Scanner class to read int value Java Tip Java Tips 0 12-03-2007 11:34 AM
Scanner class ajaymenon.k Advanced Java 1 11-26-2007 09:01 AM
JDK 5.0 Scanner Class Sircedric88 New To Java 3 07-27-2007 08:55 PM


All times are GMT +3. The time now is 03:03 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org