Java 用扫描器理解读取字符串

Java 用扫描器理解读取字符串,java,Java,我想读取一个int、double和string,并将它们添加到一个变量中,但我无法读取字符串 int i = 4; double d = 4.0; String s = "My name is "; Scanner scan = new Scanner(System.in); int i2 = scan.nextInt(); double d2 = scan.nextDouble(); String s2 = scan.nextLine(); System.out.prin

我想读取一个int、double和string,并将它们添加到一个变量中,但我无法读取字符串

int i = 4;
double d = 4.0;
String s = "My name is ";   
Scanner scan = new Scanner(System.in);
int i2 = scan.nextInt();
double d2 = scan.nextDouble();
String s2 = scan.nextLine();        
System.out.println(i+i2);
System.out.println(d+d2);
System.out.println(s + s2);
scan.close();
我的理解是scan.nextLine读取字符串,但它只是跳过它。 扫描。下一步只读取第一个单词。 如果我添加了scan.nextLine,它会正常工作;字符串str2之前=scan.nextLine;但是我不知道为什么输入

 2.4
 "Some String"
使用nextDouble时,仅拾取2.4,而不是2.4之后和某些字符串之前的EndOfLine。。 因此,您的代码通过使用2 nextLine工作

你也可以使用

int i2 = Integer.parseInt(scan.nextLine());
double d2 = Double.parseDouble(scan.nextLine());
String str2 = scan.nextLine();  

readline读取所有内容,直到下一个EOL。您可以共享输入吗?请看一看:Integer类型中的parseIntString方法不适用于参数IntThreak@joc,edited:@Lokesh Agrawal获得了它。谢谢