The basic classes used java.util.Scanner

java.util.Scanner Java5 new features, we can get the user's input through the Scanner class.

Here is the basic syntax to create a Scanner object:

Scanner scanner = new Scanner(System.in);

Next, we demonstrate a simple string input, and get the input string by next Scanner class () and nextLine () method:

// create Scanner objects

Scanner scanner = new Scanner(System.in);

 

System.out.print ( "using the next () method of receiving user inputs:");

String str = scanner.next();

System.out.println ( "user input string is:" + str);

 

// Close Object Scanner

scanner.close();

Output:

 

 


Three strings of fairy input, monster, thank separated by a space, we found that only the output of the first word: Fairy
following methods to try to use nextLine ():

// create Scanner objects

Scanner scanner = new Scanner(System.in);

 

System.out.println ( "Use nextLine () method of receiving user inputs:");

String str = scanner.nextLine();

System.out.println ( "user input string is:" + str);

 

// Close Object Scanner

scanner.close();

The output becomes:

 

 

Following is a brief summary under the next () and nextLine () the difference
next ():
1, if you only enter a carriage return, the program will continue to wait for input, to read to the end of the valid characters before they can enter;
2, next () method automatically removing the blank (whitespace / carriage return, etc.) before a valid character
3, valid only enter a blank character behind after input as the separator or terminator.
next () String with spaces can not be obtained.

nextLine ():
1, in order to Enter to end the character, that is to say nextLine () method returns all the characters before the press enter.
2, you can get blank

To enter data type int or float, in the Scanner class is also supported, but it is preferable to use hasNextXxx () method was validated before entering, reuse nextXxx () to read:

Scanner scanner = new Scanner(System.in);

// receive data entered from the keyboard:

int intNum = 0;

float floatNum = 0.0f;

System.out.print ( "receiving user input integer:");

if (scanner.hasNextInt ()) {// determines whether the input is an integer

    intNum = scanner.nextInt (); // integer receiving

    System.out.println ( "user input is an integer:" + intNum);

} else {

    // enter the wrong information

    System.out.println ( "input is not an integer!");

}

System.out.print ( "fractional receiving user input:");

if (scanner.hasNextFloat ()) {// determines whether the input is a decimal

    floatNum = scanner.nextFloat (); // receiving decimal

    System.out.println ( "fractional user input is:" + floatNum);

} else {

    // enter the wrong information

    System.out.println ( "do not enter decimals!");

}

scanner.close();

If coupled with a while loop, loop verification can also be achieved until the correct function:

Scanner input = new Scanner(System.in);

int num;

System.out.println ( "Please enter an integer numbers:");

while(!input.hasNextInt()) {

    System.out.println ( "input is not an integer, please re-enter:");

    After // hasNextInt judgment is not an integer, the cache still exists on user input

    // If a judge hasNext .. again, the outcome would still not integer

    // So here alone input.next (), in order to clear out the cache

    input.next();

}

num = input.nextInt();

System.out.println ( "integer digital input is:" + num);

Dry notes more attention to micro-channel public number: the old nine school

Guess you like

Origin www.cnblogs.com/ljxt/p/11613034.html