Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BufferedReader to take input in java

Tags:

java

I have been using the Scanner object to take in input until now and would like to learn how the BufferedReader works. I have tried it and it seems to be working only for Strings. can someone show me how to use it with ints and doubles?and how do you ask for two String inputs on the same line? Thanks.

like image 423
Daniel Cook Avatar asked Jan 25 '12 00:01

Daniel Cook


2 Answers

Think of BufferedReader and Scanner as being at different levels of abstraction, rather than interchangeable parts that "do the same thing." I think this is the fundamental issue that you're hung up on.

BufferedReader is in some sense "simpler" than Scanner. BufferedReader just reads Strings.

Scanner is much more robust than BufferedReader. It has APIs that make it easy for extracting objects of various types.

I could imagine Scanner being written using BufferedReader as an underlying building block. Whereas using Scanner to write BufferedReader would be like killing an ant with a sledgehammer.

like image 182
Greg Mattes Avatar answered Oct 07 '22 17:10

Greg Mattes


Yes, bufferedreader will take only Strings. you need to convert them into int or double as required using Integer.parseInt(value) or Double.parseDouble(value)

like image 26
pbathala Avatar answered Oct 07 '22 19:10

pbathala