Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Menu UI

Adam Sawicki
Adam Sawicki
15,967 Points

Closing Stream?

Hey,

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

regards

2 Answers

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