带升华文本扫描仪的Java不工作?

带升华文本扫描仪的Java不工作?,java,java.util.scanner,sublimetext,Java,Java.util.scanner,Sublimetext,我正在努力学习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 priva

我正在努力学习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]

我直接在编辑器中按CMD+B,运行Sublime Text 2中的代码。

您不能直接调用
nextLine()
,您需要先检查是否有可用的行(用户已点击enter键),然后使用扫描器。hasNextLine()阻塞该行,直到可以读取该行

String input ="";

if(scanner.hasNextLine()){
  input=scanner.nextLine();
}
描述了这种行为,顶部的摘要通常为您提供使用该类所需的所有信息


另外,如果您是初学者,通常建议您阅读《用Java思考》一书。

我不确定是否有精彩的文字,但您的程序看起来是正确的。我在我的NetBeans 8.0.1中运行了它,一切正常。这似乎是Sublime如何运行java程序的问题

尝试使用标准java编译器和运行工具编译并运行它。有关更多详细信息,请阅读:

另请参见:

写入和扫描

String string = read("Input: ");
只与…一起阅读

String string = read();

您是否尝试调用scanner.hasNextLine()?我在程序中添加了您的代码(替换字符串输入=scanner.nextLine();)但是,我收到了以下错误消息:MyConsoleIO.java:21:错误:找不到符号输入=scanner.nextLine();^symbol:method nextline()位置:scanner 1类型的变量scanner现在在我替换行input=scanner.nextline()中的小写“l”时出错;因此它变成了input=scanner.nextLine();,程序刚刚完成,我没有输入任何内容:为程序键入一些数据:input=[在0.9秒内完成]。因此,我仍然无法输入任何内容。这样,您将无法在if之外使用
input
(不是问题),如果有兴趣,请阅读关于变量范围的内容以了解更多信息。好的,但即使我放置行:System.out.println(“input=“+input”);在if语句中,我无法从键盘输入任何内容,我得到以下消息:为程序键入一些数据:[在0.9s中完成]Java区分大小写,nextline()和nextline()是两种不同的方法。好的。使用nextline()时,会收到以下错误消息:MyConsoleIO.java:21:error:not find symbol input=scanner.nextline();^symbol:method nextline()位置:scanner 1 error类型的变量scanner他可能是对的,升华无法允许与System.in.交互。我认为这是升华运行java程序的问题。它似乎以某种方式跳过了用户在
nextLine()
上的输入,方法抛出异常,如源代码中所述:
if(result==null)抛出新的NoSuchElementException(“找不到行”)
String string = read();