扫描仪只读取第一个单词而不是行 [英] Scanner only reads first word instead of line
本文介绍了扫描仪只读取第一个单词而不是行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我当前的程序中,一种方法要求用户输入产品描述作为 String
输入。但是,当我稍后尝试打印出这些信息时,只显示 String
的第一个单词。可能是什么原因造成的?我的方法如下:
In my current program one method asks the user to enter the description of a product as a String
input. However, when I later attempt to print out this information, only the first word of the String
shows. What could be the cause of this? My method is as follows:
void setDescription(Product aProduct) {
Scanner input = new Scanner(System.in);
System.out.print("Describe the product: ");
String productDescription = input.next();
aProduct.description = productDescription;
}
因此,如果用户输入的是带有橙味的苏打汽水,那么 System.out.print
只会产生Sparkling。
So if the user input is "Sparkling soda with orange flavor", the System.out.print
will only yield "Sparkling".
任何帮助都将不胜感激!
Any help will be greatly appreciated!
推荐答案
替换 next()
, nextLine()
:
String productDescription = input.nextLine();
这篇关于扫描仪只读取第一个单词而不是行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文