使用扫描仪类在 Java 中读取字符串输入的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18534265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issues reading string input in Java with scanner class
提问by Barney G
I'm pretty much a complete newbie when it comes to Java. I've dabbled a bit in python and VB.net, and that's about it.
当谈到 Java 时,我几乎是一个完整的新手。我在 python 和 VB.net 中有所涉猎,仅此而已。
I'm trying to write a program in java that literally reads the user's input and displays it back to them with this code:
我正在尝试用 Java 编写一个程序,该程序从字面上读取用户的输入并使用以下代码将其显示给他们:
import java.util.Scanner;
public class InputTesting
{
public static void main( String[] args )
{
Scanner input = new Scanner ( System.in );
String str1;
System.out.println("Input string: ");
str1 = input.nextString();
System.out.println( str1 );
}
}
And I get the error:
我得到错误:
InputTesting.java:13: error: cannot find symbol
str1 = input.nextString();
^
symbol: method nextString()
location: variable input of type Scanner
1 error
Can someone tell what why it's not compiling? Thanks!
有人能说出为什么它没有编译吗?谢谢!
采纳答案by christopher
input.nextString();
There is no method called nextString
in the Scanner
class. That's why you're getting the error cannot find symbol
.
没有调用方法nextString
的Scanner
类。这就是您收到错误的原因cannot find symbol
。
Try
尝试
input.nextLine(); // If you're expecting the user to hit enter when done.
input.next(); // Just another option.
回答by Rakesh KR
import java.util.Scanner;
public class InputTesting
{
public static void main( String[] args )
{
Scanner input = new Scanner ( System.in );
String str1;
System.out.println("Input string: ");
str1 = input.next();
System.out.println( str1 );
}
}
回答by Prashant Bhate
You may use one of input.nextXXX()
as detailed in Scanner javadoc. to resolve compiler error
您可以使用Scanner javadoc 中input.nextXXX()
详述的其中之一。解决编译器错误
in your case
在你的情况下
you may use input.nextLine();
get entire line of text.
您可以使用input.nextLine();
获取整行文本。
By Default whitespace delimiter used by a scanner. However If you need to scan sequence of inputs separated by a delimiter useDelimiter
can be used to set regex delim
默认情况下,扫描仪使用的空白分隔符。但是,如果您需要扫描由分隔符分隔的输入序列,useDelimiter
可用于设置正则表达式 delim
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in);) {
input.useDelimiter(";");
while (input.hasNext()) {
String next = input.next();
if ("DONE".equals(next)) {
break;
}
System.out.println("Token:" + next);
}
}
}
Input
输入
1a;2b;3c;4d;5e;DONE;
Output
输出
Token:1a
Token:2b
Token:3c
Token:4d
Token:5e
Just be cautious while scanning decimal and signed numbers as scanning is Locale
Dependent
扫描十进制和有符号数字时要小心,因为扫描是Locale
相关的
回答by user5558192
You need to rather use method nextLine()
. This methods prints entire line. By using next()
it will print only one word in your sentence.
你需要使用 method nextLine()
。此方法打印整行。使用next()
它只会在你的句子中打印一个单词。