Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Adam Sawicki15,967 Points
I wonder if @craigdennis should close the InpuStreamReader after reading the line or It isn't necessary in this case?
Alexander NikiforovJava Web Development Techdegree Graduate 22,161 Points
Hi. I think you should close
BufferedReader but not
So after quit you can put
bufferedReader.close(). For more on that topic, see for example http://stackoverflow.com/questions/1388602/do-i-need-to-close-both-filereader-and-bufferedreader
Craig DennisTreehouse Teacher
Great question! You are right, I really should be closing that stream.
Ideally that prompting would be in a class of itself and implement the AutoCloseable interface, then we could use the try-with-resources style. Or we could also create a reader in the try there and pass it as an argument to each method. Both solutions seem a little out of scope, but great idea spotting a bug!
The best idea is to probably add a finally clause to that try statement that closes the reader. The problem is if someone else calls the run method, the reader would be closed.
Thanks for pointing this out!