Receiving user input in the console without using a scanner

I would like to learn about other ways of entering data from users using other classes such as BufferedReader , etc., instead of using the Scanner class. So, was there another way to input data from the user? If so, was it effective than the Scanner class?

+4
source share
6 answers

if you use Java SE6 or higher, you can use Console clas

  Console console = System.console(); if (console==null){ System.out.print("console not available "); }else { String line = console.readLine("Enter name :"); System.out.print("your name :"+line); } 
+5
source

You can directly use System.in , for example:

 BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = r.readLine()) != null) { System.out.println(line); } 

Although it may be slightly faster than using Scanner , it is not a comparison of apples with apples: Scanner provides more methods for tokenizing input, and BufferedReader can split your input into lines, without tokenizing it.

+2
source

You can do this by following these steps: 1. Use the BufferedReader Class and wrap it with the InputStreamReader class.

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) //string str = br.readLine(); //for string input int i = Integer.parseInt(br.readLine()); // for Integer Input 

2. Now, since the readLine method throws an IOException, you need to catch it. therefore all code will look as follows.

 try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) //string str = br.readLine(); //for string input int i = Integer.parseInt(br.readLine()); // for Integer Input }catch(IOException ioe){ ioe.PrintStackTrace(); } 
+2
source

Use this:

 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); 

to create a reader for System.in , and you can use stdin.readLine() or something to get what you want.

Using BufferedReader MUCH more efficient than using Scanner .

+1
source

Here, for example ...

 InputStreamReader inStream = new InputStreamReader(System.in); BufferedReader stdin = new BufferedReader(inStream); int num , num2; String str[]=new String[2]; System.out.print("Please Enter Your First Number:"); str[0] = stdin.readLine(); System.out.print("Please Enter Your Second Number:"); str[1] = stdin.readLine(); num = Integer.parseInt(str[0]); num2 = Integer.parseInt(str[1]); 
0
source

The user can enter data during program execution without using the scanner class, and this can be done using the following program.

  class Demo { public static void main(String ar[]) { int ab = Integer.parseInt(ar[0]); int ba = Integer.parseInt(ar[1]); int res = ab+ba; System.out.print(res); } } 

This is a basic program in which the user can enter data at run time and get the desired result. You can add, subtract, multiply, divide and concatenate strings in CMD, and the user can enter data after compiling the java-program ie while calling the class file. You just need to call the class file and then enter the data after the space.

C:\Users\Lenovo\Desktop>java Demo 5 2

Here ab = 5 and ba = 2. The user can have any number or string, if he wants.

-1
source

All Articles