• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.






[
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic