Java Scanner Syntax

1. Import:

import java.util.Scanner; 

2. Create Object

Scanner scan = new Scanner (); // called scan, or in a general variable

Finally off, scan.close (); and I / O influx, temporarily unclear, the first copy.

3.next()

Read a string of characters to be read after the end of a valid input, you can not read spaces, spaces that is experiencing stopped.

        Scanner scan = new Scanner(System.in);
        String s1 = new String();
        String s2 = new String();
        String s3 = new String();
        s1 = scan.next();
        s2 = scan.next();
        s3 = scan.next();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        if(scan.hasNext())
            System.out.println("Yes");

Input: 1234567891011

Output:

123
456
789
Yes

s1 to first read a valid character encountered space 4 before the end of the first input; followed by a valid character first encounters s2 4, the end face space; S3 encountering the first valid character 7, with Li met the end of the space, this time there are unread characters in the buffer, with hasNext () judgment;

4.hasNext()

Determining whether there are data input, space or carriage return does not recognize, or will eat the transport space, carriage return empty box continuous absorbed all at once, ACM in the loop to read the next set of data.

5.nextLine()

And next () is similar, the only difference is that, next () encounters a space or carriage return on the broken, nextLine () encounters a carriage return before the break, spaces can be considered a valid character, carriage return from the beginning until the first valid character, No matter how many intermediate spaces can eat.

package my_acm;

import java.util.Scanner; 
public class MyTest10 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s1 = new String();
        String s2 = new String();
        String s3 = new String();
        String s4 = new String();
        s1 = scan.next();
        s2 = scan.next();
        s3 =scan.next (); 
        S4 = scan.nextLine (); 
        System.out.println (S1); 
        System.out.println (S2); 
        System.out.println (S3); 
        System.out.println (S4); 

        IF (scan.hasNext ()) 
            System.out.println ( "Yes1" );
         IF (scan.hasNextLine ()) 
            System.out.println ( "YES2" ); 
        
    } 
} 
/ ** input: 123,456,789,101,112 13 14 15 
output: 
123 
456 
789 
 1011 13 14 15 12 is 
S4 all of the characters behind all eat 9, and there is no unread characters 
* /
nextLine()

6.hasNextLine()

You can determine spaces and carriage returns, but will not eat any characters.

import java.util.Scanner; 
public class MyTest10 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s1 = new String();
        String s2 = new String();
        String s3 = new String();
        String s4 = new String();
        s1 = scan.next();
        s2 = scan.next();
        s3 = scan.next();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        if(scan.hasNext())
            System.out.println("Yes1");
        if(scan.hasNextLine())
            System.out.println("Yes2");
        scan.close();
    }
}
Test hasNext () will eat up space, hasNextLine () will not

import java.util.Scanner; 
public class MyTest10 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s1 = new String();
        String s2 = new String();
        String s3 = new String();
        String s4 = new String();
        s1 = scan.next();
        s2 = scan.next();
        s3 = scan.next();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        if(scan.hasNextLine())
            System.out.println("Yes2");
        if(scan.hasNextLine())
            System.out.println("Yes3");
        if(scan.hasNext())
            System.out.println("Yes1");
        scan.close();
    }
}
Test hasNext () will eat up space, hasNextLine () will not

  • Analyzing the white spaces and can be known by comparing hasNextLine (), and does not absorb the character;
  • But if the first encounter hasNext (), a space and carriage returns have all been eaten, subsequent connect hasNextLine () can not determine there are spaces and carriage returns.

7. Other types of input

nextDouble();

nextLong();

nextInt ();

nextFloat();

nextByte();

 

 

Guess you like

Origin www.cnblogs.com/shoulinniao/p/11621234.html