英文:
next() does't allow "white space" and nextLine() skips "sodaType" all together
问题
while(true) {
System.out.println("请输入一种汽水品牌。");
System.out.print("您可以选择Pepsi、Coke、Dr. Pepper或Root Beer:");
sodaType = keyboard.next();
System.out.println("sodatype:" + sodaType);
if (sodaType.equalsIgnoreCase("pepsi") || sodaType.equalsIgnoreCase("coke") ||
sodaType.equalsIgnoreCase("dr pepper") || sodaType.equalsIgnoreCase("dr. pepper") ||
sodaType.equalsIgnoreCase("root beer"))
{
System.out.println("您选择了" + sodaType);
break;
}
else {
System.out.println("请输入一个可用的汽水品牌。");
}
}
英文:
I have one problem. Neither of these work for my code.
When running this code with
> sodaType = keyboard.next();
userInput (called sodaType in code) only saves the first part of "Root Beer", outputting ("Root").
I googled the problem and
> sodaType = keyboard.nextLine();
Allows for "white space", but skips the userInput, outputting nothing, skipping the if statement.
I found different answers on this site
- https://stackoverflow.com/questions/12524243/how-do-i-make-java-register-a-string-input-with-spaces
- https://stackoverflow.com/questions/29640718/user-input-not-working-with-keyboard-nextline-and-string-java
- https://stackoverflow.com/questions/19509647/scanner-doesnt-see-after-space
I am confused on why the nextLine() worked for them, and how I should continue.
while(true) {
System.out.println("Please enter a brand of soda. ");
System.out.print("You can choose from Pepsi, Coke, Dr. Pepper, or Root Beer: ");
sodaType = keyboard.next();
System.out.println("sodatype" + sodaType);
if (sodaType.equalsIgnoreCase("pepsi") || sodaType.equalsIgnoreCase("coke") ||
sodaType.equalsIgnoreCase("dr pepper") || sodaType.equalsIgnoreCase("dr. pepper") ||
sodaType.equalsIgnoreCase("root beer"))
{
System.out.println("you chose " + sodaType);
break;
}
else {
System.out.println("Please enter an avaiable brand of soda. ");
}
}
答案1
得分: 0
因为扫描仪的工作原理是将输入分成一系列的“标记”和“分隔符”。默认情况下,“一个或多个空白字符”被视为分隔符,因此,以下输入:
Root Beer
Hello World
5
包含了 5 个标记:[Root
, Beer
, Hello
, World
, 5
]。你想要的是将其分为 3 个标记:[Root Beer
, Hello World
, 5
]。
很简单:告诉扫描仪,你希望换行符成为分隔符,而不仅仅是任意空白字符:
Scanner s = new Scanner(System.in);
s.useDelimiter("\r?\n");
这是一个正则表达式,将匹配不考虑操作系统的换行符。
在扫描仪中混合使用 nextLine()
和任何其他的 next
方法会导致困扰和痛苦,所以请不要这样做。忘记 nextLine
的存在。
英文:
That's because scanners work by separating the input into a sequence of 'tokens' and 'delimiters'. Out of the box, 'one or more whitespace characters' is the delimiter, thus, an input of:
Root Beer
Hello World
5
consists of 5 tokens: [Root
, Beer
, Hello
, World
, 5
]. What you want is that this forms 3 tokens: [Root Beer
, Hello World
, 5
].
Easy enough: Tell the scanner you intend for newlines to be the delimiter, and not just any whitespace:
Scanner s = new Scanner(System.in);
s.useDelimiter("\r?\n");
That's a regular expression that will match newlines regardless of OS.
Mixing nextLine()
with any other next method in scanner leads to pain and suffering, so, don't do that. Forget nextLine exists.
答案2
得分: -1
当您编写.next()
时,该函数会读取String
,并在遇到空白字符
之前继续读取。当您编写此代码并将输入设置为root beer
时,它只会读取root
,因为在root
之后有一个空白字符
,这告诉Java停止读取,因为用户可能希望结束读取。
sodaType = keyboard.next();
这就是为什么引入了.nextLine()
,因为它会将整行内容(包括空白字符
)都读取进来。所以,当您编写并以root beer
这样的输入:
sodaType = keyboard.nextLine();
它会将其存储为root beer
。
如果您以 root beer
这样的输入进行测试,它会将其存储为 root beer
。
注意:这包括了精确数量的空白字符。
英文:
So When you write .next()
this function reads String
and read until it encounters a white space
<br>
So when you write this and give input as root beer
it will read-only root
cause after that root
there's a white space
which tells java to stop reading cause may be user wants to end the read.
sodaType = keyboard.next();
That's the reason why .nextLint()
is introduce cause it will read the whole line as it is including white spaces
.
So when you write and gave input like root beer
sodaType = keyboard.nextLine();
it will store it as root beer
<br>
and if you gave input as root beer
it will store it as root beer
<br>
NOTE: the exact number of white spaces.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论