Skip to main content

Really annoying and simple java question about while loops and receivng double types through Scanner.

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
raithier
Offline
Joined: 2011-11-05

I am running into a very annoying problem running the jdk-6u29-windows-x64 version of java. I have also tried the 7u1 version and get the same problem. The problem is very simple, when I have a while loop and am collecting values from Scanner, the String values get skipped after getting a double value.

Here is my code and after it is the output I get from it. I have added a sleep here to try and slow it down but it still just skips over the string values. I have also tried moving the double past the String collects and it still skips.

import java.util.Scanner;

package whileproject;

import java.util.Scanner;

/**
*
* @author Home
*/
public class WhileProject {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Scanner userInput = new Scanner( System.in );

int i=0;
String name1="", name2="";
double double1=0;

while (i < 1)
{
System.out.println("Enter the value for double 1");
double1 = userInput.nextDouble();

System.out.println("Enter the value for Name 1");
name1 = userInput.nextLine();

System.out.println("Enter the value for Name 2");
name2 = userInput.nextLine();
}
// TODO code application logic here
}
}

// Output

run:
Enter the value for double 1
2
Enter the value for Name 1
Enter the value for Name 2
BUILD STOPPED (total time: 6 seconds)