Wednesday, March 23, 2022
Different ways to read user inputs in Java
- Using Scanner
Scanner in = new Scanner(System.in); String name = in.nextLine(); int number = in.nextInt();
- Using BufferReader: BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working with multiple threads. BufferedReader has a significantly larger buffer memory than Scanner. BufferedReader is a bit faster as compared to scanner because the scanner does the parsing of input data and BufferedReader simply reads a sequence of characters.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String name = reader.readLine();
- Using Console: Used to get input from the console. When used this to password, it will not be displayed to the user.
String name = System.console().readLine();
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment