This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Calculator program: BufferedReader, Stream closed

 
Ranch Hand
Posts: 132
5
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Ranchers!

I am preparing for my OCP v8 exam. I am just playing with writing simple programs. So I wrote a basic console calculator program. I made all my methods are static for my convience. I am getting a runtime exception.

Don't be afraid of size of the program. Anyone can understand it. Try it and help me to figure out what is wrong here. Cheers!




Here is the exception that is getting logged in my Eclipse console IDE
 
Ranch Hand
Posts: 125
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your readTextLine, when you read a line, you are closing the System.in stream by closing the BufferedReader (which gets closed because of try-with-resources block). So, for the next line, you get that exception.
By the way, when you get stacktrace, it shows the line numbers - which you can use to pinpoint the code causing the exception.
 
Marshal
Posts: 65084
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are doing the reading in the wrong place. Create yourself a utility class to do all your keyboard reading and never close whatever you are using to read from System.in.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!