简体   繁体   English

扫描仪不要求输入字符串

[英]Scanner not asking input of string

In the below code: 在下面的代码中:

secondI = scan.nextInt();
System.out.println("double");
secondD = scan.nextDouble();
System.out.println("string");
secondS = scan.nextLine();

After entering the double value, it is skipping the string entry part. 输入双精度值后,它将跳过字符串输入部分。 How can I fix this? 我怎样才能解决这个问题?

secondI = scan.nextInt();
System.out.println("double");
secondD = scan.nextDouble();

scan.nextLine();

System.out.println("string");
secondS = scan.nextLine();

You need to have this extra nextline statement which consumes the remaining section of that line which you entered for the double value. 您需要有一个额外的nextline语句 ,该语句将占用您为double值输入的该行的其余部分。

Use 采用

secondS = scan.next();

instead of 代替

secondS = scan.nextLine();

Please see the documentation for Scanner.nextLine() . 请参阅Scanner.nextLine()文档

Advances this scanner past the current line and returns the input that was skipped. 使该扫描仪前进到当前行之外,并返回被跳过的输入。

Try using other methods of the Scanner class such as Scanner.next() 尝试使用Scanner类的其他方法,例如Scanner.next()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM