4

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?

1
  • System.in is an InputStream. You can read from it directly or use any class that wraps it, directly or indirectly.
    – JB Nizet
    Apr 20, 2013 at 15:56

6 Answers 6

5

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);
   }
2
  • @Gowtham updated answer. but are you running this in eclipse then it might not give the system console. It is bug in eclipse Although there is workaround this here
    – ajduke
    Apr 21, 2013 at 6:47
  • If possible please add that workaround for eclipse and also the way to run this in command line to your answer. Jan 10, 2014 at 7:24
5

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

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.

1
  • Was it efficient than Scanner Class?
    – Gowtham
    Apr 20, 2013 at 16:01
2

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.

0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.