This question is linked to a previous question I asked regarding the same program, which can be viewed here:
Writing to a file code causing an endless loop
I have fixed the problem above and rewritten the function as a while loop rather than do while, but now I have the opposite problem that nothing is being written to the file. I've inserting a print statement to tell me the status of hasNextLine, and it is always returning as true even when a blank line has been entered, which is when I want the writer to terminate.
Here is the updated code:
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;;
public class Lab_Week8_WriteAStory {
public static void main(String[] args) throws FileNotFoundException {
Scanner whatToWrite = new Scanner (System.in);
PrintWriter writing = new PrintWriter ("Read and Write Files/output.txt");
while (whatToWrite.hasNextLine()){
String writeToFile = whatToWrite.nextLine();
writing.println(writeToFile);
System.out.println (whatToWrite.hasNextLine());
}
writing.close();
whatToWrite.close();
}
}
Check the documentation for Scanner.hasNextLine():
Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.
This is what's happening. Since you are using System.in as input source, the method is waiting for your input and once you provide it, it returns true and proceed to the next line and the process repeats itself.
Related
Following is the program for reversing a word using a stack. There is no problem in implementation of stack. It is executing finely. When ctrl-c is pressed, it prints the content of try-catch block twice or even thrice. What causes this?
import java.util.NoSuchElementException;
import java.util.Scanner;
class WordReversal {
public static void main(String args[]) {
StackIFace sfWord; // interface reference
Stack s = new Stack();
sfWord = s;
Scanner sc = new Scanner(System.in);
System.out.println("Control-C to stop.");
for(;;) {
String senWord = "", revWord = "";
try {
System.out.println("Enter a word: ");
senWord = sc.next();
for(int i=0;i<senWord.length();i++)
sfWord.push(senWord.charAt(i));
for(int i=0;i<senWord.length();i++)
revWord+=sfWord.pop();
System.out.println("Original Word::"+senWord+"\nReversed Word::"+revWord);
} catch(NoSuchElementException NSEe) {
System.out.println("Control-C command accepted.\nQuitting...");
//break;
}
}
}
}
One thing to notice here is if I put break (just remove two forward slashes in above code), the problem is solved but exactly what is causing this? Is Scanner causing this or is it a language problem?
NoSuchElementException is not actually "intercepting ctrl/C". So what's likely happening is you're terminating input, the next() call throws an exception because there is no input, you print a message, and loop round to do it again.
Input is still terminated, so the next() call throws an exception....
What surprises me is thus not that it loops, but that it stops looping after 2 or 3 times. Maybe some other exception gets thrown that you don't catch; hard to say without adding a handler to check.
But the root issue is: if you want that particular exception to quit the program, you need to write code that quits the program. You in fact did that when you wrote the 'break' out of the loop.
You're using an infinite for loop and not telling it when to stop.
Adding break statement is terminating the for loop. Hence, it is working.
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
I've came across the below code snippet
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
boolean flag = scan.hasNext();
while(flag){
System.out.println(scan.nextLine());
}
System.out.println("End");
}
}
The boolean flag once assigned the value true or false should not be changed again, but I see that whenever I type something in the terminal the while loop is executed again.
Can someone please explain how the flag is re-evaluated again whenever I type something in the terminal?
Note: My question is not about stopping/closing the Scanner, but its about understanding how the loop condition is re-evaluated.
I think that what you are experiencing here is scan.hasNext() blocking until your first input.
Then, the flag is always true.
Once in the loop, you print the next line, but scan.nextLine() also blocks until you provide an EOL, i.e., an input with a line end.
This is a diary program which allows you to write something in your diary (obviously). After typing enter and pressing enter, the page closes and its gonna be safed in a list. My problem is that it only runs once when I have Pages(); in the main method, so I tried this loop. It doesnt work for me and i dont know why. Need some help
import java.util.ArrayList;
import java.util.Scanner;
public class NotizbuchKlasse{
public static void Pages() {
System.out.println("day 1 : Write something in your diary.");
System.out.println("Write enter if you are done writing.");
ArrayList<String> List = new ArrayList<String>();
String ListInList;
Scanner write = new Scanner(System.in);
do {
ListInList = write.next();
List.add(ListInList);
} while (! ListInList.equals("enter"));
List.remove(List.size()-1);
write.close();
System.out.println("This is now your page. Your page is gonna be created after writing something new.");
System.out.println(List);
}
public static void main(String[]Args){
boolean run = true;
do{
Pages();
} while(run);
}
}
Error:
This is now your page. Your page is gonna be created after writing something
new.
Exception in thread "main" [hello]
day 1 : Write something in your diary.
Write enter if you are done writing.
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at NotizbuchKlasse.Pages(NotizbuchKlasse.java:12)
at NotizbuchKlasse.main(NotizbuchKlasse.java:24)
You need to check whether there is something to read before you read it. You're not currently, and that's why you're getting a NoSuchElementException.
You do this via Scanner's has* methods.
For example:
ArrayList<String> List = new ArrayList<String>();
Scanner write = new Scanner(System.in);
while (write.hasNextLine()) {
String ListInList = write.nextLine();
if (ListInList.equals("enter")) break;
List.add(ListInList);
}
// No need to remove the last item from the list.
But also, I notice that you have a loop in your main method, where you call Pages() in that loop. If you close write, you also close System.in; once a stream is closed, you can't re-open it. So if you try to read things from System.in the next time you call Pages(), the stream is already closed, so there's nothing to read.
Simply don't call write.close(). You shouldn't close streams that you didn't open in general; and you didn't open System.in (the JVM did when it started up), so don't close it.
You want to be using a while loop like this:
while (write.hasNextLine()) {
ListInList = write.nextLine();
if (doneWriting(ListInList)) { // Check for use of enter.
break; // Exit the while loop when enter is found.
}
List.add(ListInList); // No enter found. Add input to diary entry.
}
where doneWriting() is a method (that you write!) which checks to see if the user has typed enter.
Here is the documentation for the next() method of Scanner. If you read it, you will see that it throws the exception you are getting when it runs out of tokens.
If you want a little bit more a casual explanation here is a question that was asked previously about next() versus nextLine().
why does this piece of code go into an infinite loop when I try to give it a basic text file?
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TestFile
{
public static void main(String args[]) throws IOException
{
// Read in input file
File input = new File(args[0]);
Scanner freader = new Scanner(input);
while (freader.hasNextLine()) {
System.out.println("hi");
}
freader.close();
}
}
The print line just keeps going.
Because hasNexLine() does neither get the line nor change the state of the scanner. if it's true once, and no other methods of the scanner are called, it'll always be true.
Because you have to consume the nextLine so the code should be:
while ( theScanner.hasNextLine() ) {
String theLine = theScanner.nextLine();
}
If you don't invoke nextLine() you will always be watching at the same line and it will always answer true to that.
Add a call to nextLine or any other Scanner method that'll read in some input inside the while loop.
At the moment you're just repeatedly calling hasNextLine (which only returns a boolean, it doesn't modify the stream) without retrieving any input from freader, so if freader initially has another line within its input hasNextLine will always return true and your loop is essentially while (true).