Gowtham
Gowtham

Reputation: 12180

Getting input from user in Console without using Scanner

I would like to know about other ways of getting input from users using other classes like BufferedReader,etc rather than using Scanner class. So, was there any other way of getting input from the user? If so, was it efficient than Scanner class?

Upvotes: 4

Views: 65818

Answers (6)

Mavrick
Mavrick

Reputation: 505

You can do it simply by the following 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. The whole code will look like this:

    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();
    }
    

Upvotes: 5

gagan singh
gagan singh

Reputation: 11

A user can input data at the time of execution of the program without using a scanner class, and this can be done by 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 where a user can input data at the time of execution and get the desired result. You can add, subtract, Multiply, divide and concatenate strings, in CMD and a user can input data after compiling the java program i.e at the time of calling the class file. You just need to call the class file and then enter the data after a space.

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

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

Upvotes: -1

Edie
Edie

Reputation: 65

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]);

Upvotes: 0

ajduke
ajduke

Reputation: 5057

if you are using the Java SE6 or higher then you can make use of 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);
   }

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726889

You can use System.in directly, like this:

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

Although this may be a little faster than using the Scanner, it's not an apples-to-apples comparison: Scanner provides more methods for tokenizing the input, while BufferedReader can split your input into lines, without tokenizing it.

Upvotes: 3

yzn-pku
yzn-pku

Reputation: 1082

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 a BufferedReader is MUCH more efficient than using a Scanner.

Upvotes: 2

Related Questions