First, the source code
/*
To receive user input numbers on the console
by csdn--Yue Procedure-Siege Lion
2014.11.25
*/
Import Java.util.Scanner;
public class receiveinput{
public static void Main (string[] args) {
Scanner scan = new Scanner (system.in); Create a scanner scanner to encapsulate the in input stream of the system class
System.out.println ("Please enter your name:");
String name = Scan.nextline (); Get one line of text
System.out.println ("Please enter your Age:");
int age = Scan.nextint (); Get integer input
System.out.println ("Please enter your height:");
Double stature = scan.nextdouble (); Get double type input
SYSTEM.OUT.PRINTLN ("Data input complete ...") ");
System.out.println (age+ "+name+" in the vicinity of the neighborhood in the Xiaohegou last night, \ n "+" height "+stature+" cm, he can not afford to withstand this cold trouble. ");
}
}
Ii. Summary of Knowledge
1. Java.util.Scanner class
A. The main function is to simplify text scanning. The Read method provided by system.in reads data by byte.
When a Scanner is created via new Scanner (system.in), the console waits for input until the ENTER key is finished, passing the input to Scanner as the scanned object. If you want to get the input, you only need to call the scanner Nextline () method.
such as an instance program.
B. Common methods:
Common methods of Scanner class
Serial number |
Method |
Type |
Description |
1 |
Public Scanner (File source) throws FileNotFoundException |
Structure |
Receive content from a file |
2 |
Public Scanner (InputStream source) |
Structure |
Input from the specified byte Receive content in stream |
3 |
public boolean hasnext (pattern pattern) |
Ordinary |
The input data is judged to be No conformance to specified regular criteria |
4 |
public boolean hasnextint () |
Ordinary |
Determines whether the input is an integer |
5 |
public boolean hasnextfloat () |
Ordinary |
Determine if the input is a decimal |
6 |
Public String Next () |
Ordinary |
Receive content |
7 |
Public String Next (pattern) |
Ordinary |
Receive content, perform regular validation |
8 |
public int Nextint () |
Ordinary |
Receive numbers |
9 |
public float nextfloat () |
Ordinary |
Receive decimal |
10 |
Public Scanner Usedelimiter (String pattern) |
Ordinary |
Set the Read separator |
The scanner class can receive any input stream.
In the scanner class, a construction method that can receive a inputstream type is provided, which means that as long as the subclasses of the byte input stream can be read conveniently by the scanner class.
Example: Scanner uses spaces as delimiters to separate text, but allows you to specify new delimiters
Program:
public static void Main (string[] args) throws FileNotFoundException {
& nbsp; Scanner s = new Scanner (" 123 asdf SD 789 SDF asdfl,sdf.sdfl,asdf ......asdfkl las ");
S.usedelimiter ("|,|\\.");
while (S.hasnext ()) {
System.out.println (S.next ());
}
}
Remove the comment line and use a space or comma or dot number as the separator.
2.BufferReader class
A.bufferreader is located in the Java.io package, the reading data is more fixed, so the format is relatively simple.
Instantiated:
Bufferreader br = new Bufferreader (new InputStreamReader (system.in));
Read data by ReadLine (), ReadLine () to read one row of data by pressing ENTER, as long as the return key is ReadLine ();
Using Bufferreader to enter data of a type other than a character is relatively cumbersome and requires some xxxx.parsexxx () to convert the corresponding data type.
Concrete Visible: http://book.51cto.com/art/200907/140914.htm , this is more systematic.