wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Trouble with a scanner method... Really want to understand this problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with a scanner method... Really want to understand this problem " Watch "Trouble with a scanner method... Really want to understand this problem " New topic
Author

Trouble with a scanner method... Really want to understand this problem

Jamal Taylor
Greenhorn

Joined: Sep 06, 2012
Posts: 20


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.


Sorry.






[
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Jamal Taylor
Greenhorn

Joined: Sep 06, 2012
Posts: 20
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Jamal Taylor
Greenhorn

Joined: Sep 06, 2012
Posts: 20


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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble with a scanner method... Really want to understand this problem