java等待输入密钥解决方案不起作用

0h4hbjxa  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(429)

所以我试着实现一个解决方案,等待用户按enter键继续:java控制台在继续之前提示输入enter
但是,当我尝试使用建议的解决方案时,我会遇到以下错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at printLine.promptEnterKey(printLine.java:153)
at printLine.main(printLine.java:144)

java.io.IOException: Stream closed
at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:176)
at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:342)
at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
at printLine.promptEnterKey(printLine.java:155)
at printLine.main(printLine.java:146)

我以前做过一个测试程序,以确保它能像预期的那样工作,而且对我来说效果很好:

// import scanner for user-input
import java.util.Scanner;

public class test{

    public static void promptEnterKey(){

        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();

    }

    public static void main(String[] args){

        promptEnterKey();

    }

}

这是我的代码,我正在编写一个程序,该程序将读取当前行,打印它,然后等待用户按enter键,然后继续:

// import System.out
import static java.lang.System.out;

// import scanner for user-input
import java.util.Scanner;

// import File class
import java.io.File;

// import FileNotFoundException
import java.io.FileNotFoundException;

// delete if ioexception not used
import java.io.IOException;

/* this is the main public class */
public class printLine {

  /* this method is used to execute the application */
  public static void main(String[] args){

    // create scanner for user input
    Scanner userInput = new Scanner(System.in);

    // user input for file
    out.println("This program will print the text file line by line, waiting for the user to hit the enter key");
    out.println("Please specify the file to print line by line: ");
    String textFile = userInput.nextLine();
    userInput.close();

    // try to open file
    try{

      // load the file
      File text = new File(textFile);

      // for reading the file
      Scanner textReader = new Scanner(text);

      // while there is another token...
      while (textReader.hasNextLine()){

        String curLine = textReader.nextLine();
        out.println(curLine);

        promptEnterKey();

      }// end while

      // close reader
      textReader.close();

    }// end try

    // catch FileNotFoundException error
    catch (FileNotFoundException e){

      out.println("File not found.");
      e.printStackTrace();

    }// end catch

  }// end main

  /* This method is for waiting for the user to press the Enter key.
     this was taken from https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on */
  public static void promptEnterKey(){

    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();

  }

}// end class

下面是我尝试使用的示例文本摘录:

There was nothing so VERY remarkable in that; nor did Alice 
think it so VERY much out of the way to hear the Rabbit say to 
itself, `Oh dear!  Oh dear!  I shall be late!'  (when she thought 
it over afterwards, it occurred to her that she ought to have 
wondered at this, but at the time it all seemed quite natural); 
but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT- 
POCKET, and looked at it, and then hurried on, Alice started to 
her feet, for it flashed across her mind that she had never 
before seen a rabbit with either a waistcoat-pocket, or a watch to 
take out of it, and burning with curiosity, she ran across the 
field after it, and fortunately was just in time to see it pop 
down a large rabbit-hole under the hedge. 
In another moment down went Alice after it, never once 
considering how in the world she was to get out again. 
The rabbit-hole went straight on like a tunnel for some way, 
and then dipped suddenly down, so suddenly that Alice had not a 
moment to think about stopping herself before she found herself 
falling down a very deep well. 
Either the well was very deep, or she fell very slowly, for she 
had plenty of time as she went down to look about her and to 
wonder what was going to happen next.  First, she tried to look 
down and make out what she was coming to, but it was too dark to 
see anything; then she looked at the sides of the well, and 
noticed that they were filled with cupboards and book-shelves; 
here and there she saw maps and pictures hung upon pegs.  She 
took down a jar from one of the shelves as she passed; it was 
labelled `ORANGE MARMALADE', but to her great disappointment it 
was empty:  she did not like to drop the jar for fear of killing 
somebody, so managed to put it into one of the cupboards as she 
fell past it.

任何关于我做错了什么的见解都是值得赞赏的。谢谢!
jt公司

wqnecbli

wqnecbli1#

您正在关闭 userInput (System.in) 读取文件名后立即 userInput.close(); 把那行移到代码的末尾
如果scanner.close实现了可关闭的接口(在您的情况下是system.in input stream:https://www.tutorialspoint.com/java/util/scanner_close.htm

相关问题