Instance Program:
1, the use of Scanner to read from the keyboard integer or float data
Copy Code code as follows:
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's a try. Enter string data, but there are spaces in the middle that cannot be displayed, and the scanner class does not yet have this functionality
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:"); For loop receives int type data
int b = In.nextint ();
SYSTEM.OUT.PRINTLN ("The int number is:" + b);
}
}
}
2, using BufferedReader to read the string from the keyboard and write into the file Abc.txt
Copy Code code as follows:
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 ();
}
}
A description of the JDK1.5 scanner class
Scanner is a new class for SDK1.5, but use this class to create an object.
Scanner reader=new Scanner (system.in);
The reader object then invokes the following methods (functions) to read the various data types entered by the user at 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 read 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 use it directly because the Read method provided by System.in can only read one byte of data at a time. And we usually use to read a string or a number, so the Read method provides a function that is not much use for us.
In Java SE 6, you can use the Scanner class to get input from the user, Scanner class is located in the Java.util package, and if you want to use Scanner to obtain user input, add import java.util.Scanner; This statement. 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:
Copy Code code as follows:
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 ());
}
}
Running the above program, you will see that the string you entered will be displayed as shown below.
Let's take a look at the meaning of each statement in this program:
New is to create an object, in which the new meaning is to create an object scan of the scanner class. However, when you create an object of the scanner class, you need to use system.in as its argument, or scanner as a supporter of the System.in object, System.in get the user input content, give scanner to do some processing.
Several methods are available in the Scanner class:
Next (): Gets a string;
Nextint (): Converts the obtained string to an integer of type int;
Nextfloat (): Converts the obtained string into a float type;
Nextboolean (): Converts the obtained string into a Boolean type;
It is very convenient to use scanner to get input from users, but scanner get input based on spaces, including SPACEBAR, tab and enter. When you press any of these keys, scanner returns the next input. When you include spaces in the middle of your input, it is clear that using scanner does not completely get the string you entered. At this point we can consider using the BufferedReader class to get input. In fact, in Java SE 1.4 and previous versions, no scanner method has been provided, We also use Bufferreader when we get input.
The BufferedReader class is in the Java.io package, so to use this class, you 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 get input is much more complicated to understand. But using this method is fixed, and you can do it before each use.
BufferedReader buffer = new BufferedReader (new InputStreamReader (system.in));
String text = Buffer.readline ();
The ReadLine () method returns all character input before the user presses the ENTER key, excluding the last pressed enter return character.
The complete sample program is as follows:
Copy Code code 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 of strings");
String text = Buffer.readline ();
System.out.println ("The string you entered is:" + text);
}
}
4, as shown in the following program: Class Stringtest
Copy Code code as follows:
{
public static void Main (string[] args)
{
System.out.println (Args[0]);
System.out.println (Args[1]);
System.out.println (args[2]);
}
}
After executing the statement namely: Java + class name input content, that will be received by args,
Because the args is the one that receives the command line arguments.