Example program:
1. Use Scanner to read integer or float data from the keyboard
2. Use BufferedReader to read a string from the keyboard and write it into the file abc.txt
Description of JDK1.5 Scanner class
Scanner is a new class added in SDK1.5, but you can use this class to create an object.
Scanner reader=new Scanner(System.in);
Then the reader object calls the following methods (functions) to read various data types entered by the user on the command line:
next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()
Use the nextLine() method to enter a line that may contain spaces. If you are reading a word, you can call the .next() method
3. The difference between Scanner and BufferedReader
When entering data into a program in command line mode, we can use the standard input string object System.in. However, we do not often use it directly because the read method provided by System.in can only read one word at a time. section of data, and what we usually use is to read a string or a number, so the functions provided by the read method are not of much use to us.
In Java SE 6, you can use the Scanner class to obtain user input. The Scanner class is located in the java.util package. If you want to use Scanner to obtain user input, you must add the import java.util.Scanner; statement of .import The function is to tell the compiler that you will use the Scanner class in the java.util package.
Let's look at an example:
Run the above program and you will see that the string you entered will be displayed as it is below.
Let's take a look at the meaning of each statement in this program:
new means creating an object. In the program, new means creating an object scan of the Scanner class. But when creating an object of the Scanner class, you need to use System.in as its parameter. Scanner can also be regarded as System.in The object's supporter, System.in, obtains the content input by the user and hands it to Scanner for some processing.
Several methods are provided in the Scanner class:
next(): Get a string;
nextInt(): Convert the obtained string into an integer of type int;
nextFloat(): Convert the obtained string into float type;
nextBoolean(): Convert the obtained string into boolean type;
It is very convenient to use Scanner to obtain user input, but Scanner obtains input based on the space character, including the space key, Tab key and Enter key. When any of these keys is pressed, Scanner will return to the next input. When you enter When there are spaces in the middle of the content, obviously, using Scanner cannot completely obtain the string you input. At this time, we can consider using the BufferedReader class to obtain the input. In fact, in Java SE In versions 1.4 and earlier, the Scanner method is not yet provided, and we also use BufferReader when obtaining input.
The BufferedReader class is located in the java.io package, so to use this class, you must 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 BufferedReader to obtain input is much more complicated to understand. But this method is fixed, just follow the same method before each use.
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String text = buffer.readLine();
The readLine() method will return all the characters entered by the user before pressing the Enter key, excluding the last returned character of the Enter key.
The complete sample program is as follows:
}
4. As shown in the following program: class StringTest
Enter content after the execution statement: java + class name, and it will be received by args.
Because args receives command line parameters.