Multiple implementations of Java keyboard input

  • 2020-04-01 01:35:04
  • OfStack

Example program:
1. Use Scanner to read integer or float data from the keyboard


//import java.io.*;
import java.util.*;
public class InputTest{
public static void main(String[] args){
  Scanner in = new Scanner(System.in);    //Scanner class
  System.out.println("Please input a float number:");
  float a = in.nextFloat();    //Receive float data
  System.out.println("Please input a string: ");    // Here we try the input String Data, but you can't show it with a space, Scanner class Not yet 
  Scanner str = new Scanner(System.in);
  System.out.println("The string is :" + str.next());
  System.out.println("The float number is: " + a);
  for(int i = 0;i < 4;i++){
   System.out.println("Please input a int number: ");   //The for loop receives int data
   int b = in.nextInt();
   System.out.println("The int number is: " + b);
  }
}
}

2. Use BufferedReader to read the string from the keyboard and write it into the file abc.txt


import java.io.*;
public class InputTest{
public static void main(String[] args) throws IOException{
  BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
  BufferedWriter buf2 = new BufferedWriter(new FileWriter("abx.txt"));
  String str = buf.readLine();
  while(!str.equals("exit")){
   buf2.write(str);
   buf2.newLine();
   str = buf.readLine();
  }
  buf.close();
  buf2.close();
}
}

Description of the JDK1.5 Scanner class

Scanner is a new class in SDK1.5, but it is used to create an object.
Scanner reader = new Scanner (System. In);

The reader object then calls the following methods (functions) to read the various data types entered by the user at the command line:
The next Byte (), nextDouble (), nextFloat, nextInt (), nextLine (), nextLong (), nextShot ()
The nextLine() method may contain Spaces in the input line. If a word is read, the.next() method may be called


3. Difference between Scanner and BufferedReader
In command line mode to input data to the program, we can use the standard input string object System. In. However, we do not often use it directly, because of the System. In providing the read method can only read a byte of data at a time, and we usually used is usually reads a string or a number, so the read method so provide, is not much use for us.
In Java SE 6, you can use the Scanner class to get the user's input. The Scanner class is in the java.util package. The function of import is to tell the compiler that you will use the Scanner class in the java.util package.
Let's take a look at an example:


import java.util.Scanner;
public class TestScanner{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println(" Please enter a string :");
System.out.println(" The string you entered is :" + scan.next());
}
}

Run the above program and you will see that the string you entered will be displayed exactly as it is below.
Let's look at the meaning of each statement in this program:
New is to create an object, in the program new means to create an object of Scanner class scan. But when the object of Scanner class is created, it needs to use System. In as its parameter, you can also think of Scanner as the supporter of System.
Several methods are provided in the Scanner class:
Next (): gets a string;
NextInt (): converts the obtained string to an int;
NextFloat (): converts the obtained string to a float;
NextBoolean (): converts the obtained string to a Boolean;

Use Scanner to obtain the user's input is very convenient, but the basis of a Scanner to obtain input is Spaces, including the blank space key, the Tab key and Enter key. When press the Ren Yijian, Scanner will return the next input. When you input the content of the intermediate including Spaces, obviously, using Scanner will not be able to complete the string you input. This time we can consider're using the BufferedReader input class. Actually in Java SE In 1.4 and earlier versions, there was no Scanner method and we used BufferReader to get the input.
The BufferedReader class is in the java.io package, so to use it, introduce the java.io package :import java.io.bufferedreader.
The readLine() method using the BufferedReader object must handle the java.io.ioexception Exception (Exception).
Using a BufferedReader to get input is much more complex to understand, but it is fixed and can be done before each use.
BufferedReader buffer = new BufferedReader(new InputStreamReader(system.in));
A String of text = buffer. ReadLine ();
The readLine() method returns all characters the user entered before the Enter key was pressed, except the last Enter return character.
The complete sample program is as follows:


import java.io.BufferedReader;
public class TestBufferedReader{
public static void main(String[] args) throws IOException{
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" Please enter a string ");
String text = buffer.readLine();
System.out.println(" The string you entered is :" + text);
}
}

4. The following program is shown: class StringTest


{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
}
}

After the execution statement that is: Java + class name, the input content will be received by args,
Because args receives command-line arguments.


Related articles: