0

I read somewhere using bufferedReader is much faster than reading an input using a scanner. This would be beneficial when completing coding problems such as the ones available on DMOJ (which have time constraints). How would I read the input of int's and strings using a bufferedReader instead?

My current method:

Scanner input = new Scanner (System.in);     //initializing scanner

String ____ = input.nextLine();              //reading a string input
int ____ = input.nextInt();                  //reading an int input
Bipon Roy
  • 53
  • 5
  • You will not get time advantage when reading from a keyboard input. – PM 77-1 Dec 13 '19 at 00:06
  • It's part of being "fast": `BufferedReader` does not do that. You would get an entire line, then blow it up with `split()`, then convert whatever parts need to be converted (`Integer.parseInt()` and the like). Being "buffered" has its charm, but you can do that with a stream too, wrap it into `BufferedInputStream`. – tevemadar Dec 13 '19 at 00:11
  • @PM77-1 ,the program will be reading inputs from an online judge so I wouldn't be putting in the inputs myself. – Bipon Roy Dec 13 '19 at 00:22
  • @tevemadar I will look into BufferedInputStream's. – Bipon Roy Dec 13 '19 at 00:23
  • Performance wlll be dominated by the code of your solution to the problem, not by I/O of the input. – user207421 Dec 13 '19 at 03:49
  • Refer:https://stackoverflow.com/a/2231399/2987755, for Q: https://stackoverflow.com/q/2231369/2987755 – dkb Dec 13 '19 at 04:16

1 Answers1

-1

Like this

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");

String name = reader.readLine();
System.out.println("Your name is: " + name);

You need to convert them manually based on the type of input you expect to receive.

Ex: For int you do Integer.parseInt(inputValue)

Sunil Dabburi
  • 1,442
  • 12
  • 18