So I've tried to implement a solution for waiting on user to press enter to continue found here: Java Console Prompt for ENTER input before moving on
However, when I try to use either of the solutions proposed I get the following errors:
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)
and
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)
I made a test program before to make sure this would work as expected and it worked fine for me:
// 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();
}
}
Here's my code, I am working on a program that will read the current line, print it and then wait for the user to hit enter before proceeding:
// 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
Here's the sample text excerpt that I am trying to use it on:
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.
Any insight as to what I'm doing wrong is appreciated. Thanks!
JT
you are closing the userInput (System.in)
immediately after reading the file name userInput.close();
move that line to the end of your code
Scanner.close will also close it's underlying readable if it implements Closable interface, in your case that is System.in input stream: https://www.tutorialspoint.com/java/util/scanner_close.htm