This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(14个回答)
4年前关闭。
我从用户那里读取了一个字符串。然后,当我使用字符串时,我看到它正在被缩短。我不知道如何或为什么?我需要整个
这是代码:
运行示例:
示例输出为:
(14个回答)
4年前关闭。
我从用户那里读取了一个字符串。然后,当我使用字符串时,我看到它正在被缩短。我不知道如何或为什么?我需要整个
String
,而不仅仅是其中的一部分!这是代码:
String exp=null;
System.out.println("Enter an expression without nested parenthesis");
try {
exp=read.next();
System.out.println("exp 1 : " + exp);
}
运行示例:
input: 13 + 45
示例输出为:
exp 1 : 13
最佳答案
您需要阅读整行:
String exp = null;
System.out.println("Enter an expression without nested parenthesis");
try {
exp = read.nextLine();
System.out.println("exp 1 : "+exp);
}
read.next
仅读取直到第一个空格。