【发布时间】:2015-06-02 01:46:42
【问题描述】:
我正在努力学习 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。
【问题讨论】:
-
你试过打电话给
scanner.hasNextLine()吗?
标签: java java.util.scanner sublimetext