【问题标题】:Java with Sublime Text - Scanner not working? [closed]带有 Sublime Text 的 Java - 扫描仪不工作? [关闭]
【发布时间】: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


【解决方案1】:

我不确定 Sublime Text,但您的程序看起来是正确的。我在我的 NetBeans 8.0.1 中运行它,一切正常。看来是Sublime运行java程序的问题。

尝试使用标准的 java 编译器和运行工具编译和运行它。更多详情请阅读:http://www.oracle.com/technetwork/java/compile-136656.html

另请参阅: https://stackoverflow.com/a/14810204/1981450

【讨论】:

  • 他可能是对的,sublime 不允许与 System.in 交互。
  • 我认为这是如何 sublime 运行java 程序的问题。看起来它以某种方式跳过了nextLine() 上的用户输入,并且方法抛出异常,如源代码中所述:if (result == null) throw new NoSuchElementException("No line found");
【解决方案2】:
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();

【讨论】:

    【解决方案3】:

    您不能直接调用nextLine(),您需要先检查一行是否可用(用户按回车键),scanner.hasNextLine() 会阻塞直到可以读取该行。

    String input ="";
    
    if(scanner.hasNextLine()){
      input=scanner.nextLine();
    }
    

    javadoc for Scanner 描述了这种行为,顶部的摘要通常会为您提供使用该类所需的所有信息。

    另外,如果你是初学者,通常推荐Thinking in Java一书。

    【讨论】:

    • 我在程序中添加了您的代码(替换 String input =scanner.nextLine(); )但是,我收到了这个错误消息:MyConsoleIO.java:21: error: cannot find symbol input=scanner.nextline (); ^ 符号:方法 nextline() 位置:类型为 Scanner 1 的变量扫描仪错误 现在,当我替换行中的小写“l”时 input=scanner.nextline();这样它就变成了 input=scanner.nextLine();,程序刚刚完成,我没有输入任何内容:为程序键入一些数据:input = [Finished in 0.9s]。所以我仍然无法输入任何内容。
    • 这样您将无法在 if 之外使用input(这不是问题),如果有兴趣,请阅读变量范围了解更多信息。跨度>
    • 好的,但即使我放置行: System.out.println("input = " + input);在 if 语句中,我无法从键盘输入任何内容,我收到以下消息:为程序键入一些数据:[在 0.9 秒内完成]
    • Java 区分大小写,nextline() 和 nextLine() 是两种不同的方法。
    • 好的。当我使用 nextline() 时,我收到以下错误消息: MyConsoleIO.java:21: error: cannot find symbol input=scanner.nextline(); ^ 符号:方法 nextline() 位置:类型为 Scanner 1 错误的变量扫描仪
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2016-11-29
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多