This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Alright, I am having serious trouble. Okay maybe not serious, but not understanding this is causing me unnecessary stress. Went out to drink tonight, and I could not enjoy myself, because of this.
Now the program creates a text file, writes data into it, and then reads the data to the user. Simple enough, but the problem is I get this error "Exception in thread "main" java.lang.IllegalStateException: Scanner closed". The program works it writes the data, displays the data, but it gives me this error. Now I have narrowed down the error the readFile() method near my while loop. If I close the scanner in the loop it does what I require it to do, but if I close the scanner outside the loop I do not get the exception, but it does not write the data into the text file. I know that I have to close the scanner, because it' not a stream, but easier said then done. Can someone please give me guidance on this issue. I have been to numerous sites, but haven't gotten a solid explanation on this.
No, you don't have to close the scanner. And if you do close it, then you have to do it after you're done reading from it. You can't close it in the loop and then go back for another pass round the loop and use the scanner you just closed.
In the future, please UseCodeTags(←click) when posting code, and restrict your use of color to a bare minimum for emphasis. As it stands, your code is too painful to read.
Joined: Sep 06, 2012
The odd thing is the only time the code will actually work is if I close the scanner inside the loop. Anything else I do results in the program not writing the data to the text file. I don't understand why.
Looks like you're trying to write to the same file you read from. You write to the file without first closing the input stream (in this case, that basically amounts to closing the scanner) that's reading from the file.
If your loop is:
then you can't do it using just one file.
If your approach is
Then you have to close the input stream / scanner between the read loop and the write loop, NOT inside the read loop. You seem to be saying that doesn't work, but without seeing exactly what you tried there, it's impossible to say where you went wrong.
Joined: Sep 06, 2012
It writes to the text file but it gives me the error Exception in thread "main" java.lang.IllegalStateException: Scanner closed. Can I give other information that might help you understand.
I don't know what else I can tell you beyond what I already have. Again: You can't use a scanner once you've closed it, so if you're using to read in a loop, the close() has to be after the loop is done. No matter how much you say it "seems to work" when you close the scanner inside the loop, you can't do it that way--it's not actually working.
Create an SSCCE(←click) that does ONLY with just a couple of lines, no user prompting, etc. Once you get it working, gradually add pieces--testing after each small addition--until you have your entire program. If you can't get it working, post that code.