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.

Java Java Data Structures Efficiency! Menu UI

Adam Sawicki
Adam Sawicki
15,967 Points

Closing Stream?


I wonder if @craigdennis should close the InpuStreamReader after reading the line or It isn't necessary in this case?


2 Answers

Craig Dennis
Craig Dennis
Treehouse 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!