Getting input from user in Console without using S

2019-05-08 13:27发布

问题:

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:

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:

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.



回答3:

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


回答4:

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.



回答5:

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


回答6:

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.