简体   繁体   中英

Reading an input using a bufferedReader compared to a Scanner (System.in)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM