Java 使用 Sublime Text - 扫描仪不工作?
Java with Sublime Text - Scanner not working?
我正在努力学习 Java,我才刚刚开始。我想 运行 我在网上找到的这段代码:
import java.util.Scanner; // needed for Scanner
/** A Java program that demonstrates console based input and output. */
public class MyConsoleIO
{
// Create a single shared Scanner for keyboard input
private static Scanner scanner = new Scanner( System.in );
// Program execution starts here
public static void main ( String [] args )
{
// Prompt the user
System.out.print( "Type some data for the program: " );
// Read a line of text from the user.
String input = scanner.nextLine();
// Display the input back to the user.
System.out.println( "input = " + input );
} // end main method
} // end MyConsoleIO class
但是我得到这个错误:
Type some data for the program:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at MyConsoleIO.main(MyConsoleIO.java:17)
[Finished in 0.9s with exit code 1]
我正在 运行在 Sublime Text 2 中编辑代码,直接在编辑器中,按 CMD+B。
您不能直接调用 nextLine()
,您需要先检查一行是否可用(用户已按回车键),scanner.hasNextLine()
会阻塞直到可以读取该行。
String input ="";
if(scanner.hasNextLine()){
input=scanner.nextLine();
}
javadoc for Scanner 描述了这种行为,顶部的摘要通常会为您提供使用该 class 所需的所有信息。
此外,如果您是初学者,通常推荐 Thinking in Java 这本书。
我不确定 Sublime Text,但你的程序看起来是正确的。我 运行 它在我的 NetBeans 8.0.1 中并且一切正常。看起来这是 Sublime 运行s java 程序的问题。
尝试使用标准 java 编译器和 运行 工具编译和 运行 它。有关详细信息,请阅读:http://www.oracle.com/technetwork/java/compile-136656.html
另请参阅:
private String read(String text) {
System.out.println(text);
return read();
}
private String read() {
if (!scan.hasNext())
System.exit(0);
return scan.next();
}
使用
写入和扫描
String string = read("Input: ");
只阅读...
String string = read();
我正在努力学习 Java,我才刚刚开始。我想 运行 我在网上找到的这段代码:
import java.util.Scanner; // needed for Scanner
/** A Java program that demonstrates console based input and output. */
public class MyConsoleIO
{
// Create a single shared Scanner for keyboard input
private static Scanner scanner = new Scanner( System.in );
// Program execution starts here
public static void main ( String [] args )
{
// Prompt the user
System.out.print( "Type some data for the program: " );
// Read a line of text from the user.
String input = scanner.nextLine();
// Display the input back to the user.
System.out.println( "input = " + input );
} // end main method
} // end MyConsoleIO class
但是我得到这个错误:
Type some data for the program:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at MyConsoleIO.main(MyConsoleIO.java:17)
[Finished in 0.9s with exit code 1]
我正在 运行在 Sublime Text 2 中编辑代码,直接在编辑器中,按 CMD+B。
您不能直接调用 nextLine()
,您需要先检查一行是否可用(用户已按回车键),scanner.hasNextLine()
会阻塞直到可以读取该行。
String input ="";
if(scanner.hasNextLine()){
input=scanner.nextLine();
}
javadoc for Scanner 描述了这种行为,顶部的摘要通常会为您提供使用该 class 所需的所有信息。
此外,如果您是初学者,通常推荐 Thinking in Java 这本书。
我不确定 Sublime Text,但你的程序看起来是正确的。我 运行 它在我的 NetBeans 8.0.1 中并且一切正常。看起来这是 Sublime 运行s java 程序的问题。
尝试使用标准 java 编译器和 运行 工具编译和 运行 它。有关详细信息,请阅读:http://www.oracle.com/technetwork/java/compile-136656.html
另请参阅:
private String read(String text) {
System.out.println(text);
return read();
}
private String read() {
if (!scan.hasNext())
System.exit(0);
return scan.next();
}
使用
写入和扫描String string = read("Input: ");
只阅读...
String string = read();