[英]Issues reading string input in Java with scanner class
I'm pretty much a complete newbie when it comes to Java.说到 Java,我几乎是一个完全的新手。 I've dabbled a bit in python and VB.net, and that's about it.我已经涉足了 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!谢谢!
input.nextString();
There is no method called nextString
in the Scanner
class. Scanner
类中没有名为nextString
方法。 That's why you're getting the error cannot find symbol
. 这就是为什么你得到的错误cannot find symbol
。
Try 尝试
input.nextLine(); // If you're expecting the user to hit enter when done.
input.next(); // Just another option.
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 );
}
}
You may use one of input.nextXXX()
as detailed in Scanner javadoc . 您可以使用input.nextXXX()
javadoc中详述的input.nextXXX()
之一。 to resolve compiler error 解决编译器错误
in your case 在你的情况下
you may use input.nextLine();
你可以使用input.nextLine();
get entire line of text. 得到整行文字。
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
可用于设置正则表达式分隔符
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
Dependent
You need to rather use method nextLine()
. 您需要使用方法nextLine()
。 This methods prints entire line. 此方法打印整行。 By using next()
it will print only one word in your sentence. 通过使用next()
,它将在您的句子中只打印一个单词。
// That's how i managed solution for input a string // 这就是我管理输入字符串的解决方案的方式
import java.util.Scanner;导入 java.util.Scanner; public class PalindromeWord {公共课回文词{
public static void main(String[] args) {
//System.out.println("Enter a string: ");
Scanner input = new Scanner(System.in);
String S = input.next();
String reverseStr = "";
int strLength = S.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + S.charAt(i);
}
if (S.equals(reverseStr)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.