Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculator program: BufferedReader, Stream closed  RSS feed

 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • 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
 
Salil Wadnerkar
Ranch Hand
Posts: 90
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!