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

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

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

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

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

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

  1. // import scanner for user-input
  2. import java.util.Scanner;
  3. public class test{
  4. public static void promptEnterKey(){
  5. Scanner scanner = new Scanner(System.in);
  6. scanner.nextLine();
  7. }
  8. public static void main(String[] args){
  9. promptEnterKey();
  10. }
  11. }

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

  1. // import System.out
  2. import static java.lang.System.out;
  3. // import scanner for user-input
  4. import java.util.Scanner;
  5. // import File class
  6. import java.io.File;
  7. // import FileNotFoundException
  8. import java.io.FileNotFoundException;
  9. // delete if ioexception not used
  10. import java.io.IOException;
  11. /* this is the main public class */
  12. public class printLine {
  13. /* this method is used to execute the application */
  14. public static void main(String[] args){
  15. // create scanner for user input
  16. Scanner userInput = new Scanner(System.in);
  17. // user input for file
  18. out.println("This program will print the text file line by line, waiting for the user to hit the enter key");
  19. out.println("Please specify the file to print line by line: ");
  20. String textFile = userInput.nextLine();
  21. userInput.close();
  22. // try to open file
  23. try{
  24. // load the file
  25. File text = new File(textFile);
  26. // for reading the file
  27. Scanner textReader = new Scanner(text);
  28. // while there is another token...
  29. while (textReader.hasNextLine()){
  30. String curLine = textReader.nextLine();
  31. out.println(curLine);
  32. promptEnterKey();
  33. }// end while
  34. // close reader
  35. textReader.close();
  36. }// end try
  37. // catch FileNotFoundException error
  38. catch (FileNotFoundException e){
  39. out.println("File not found.");
  40. e.printStackTrace();
  41. }// end catch
  42. }// end main
  43. /* This method is for waiting for the user to press the Enter key.
  44. this was taken from https://stackoverflow.com/questions/26184409/java-console-prompt-for-enter-input-before-moving-on */
  45. public static void promptEnterKey(){
  46. Scanner scanner = new Scanner(System.in);
  47. scanner.nextLine();
  48. }
  49. }// end class

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

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

相关问题