Java Scanner語法


1.導入:

import java.util.Scanner; 

2.創建對象

Scanner scan = new Scanner(System.in);//一般變量名為scan或者in

最后關閉,scan.close();和I/O流有關,暫不清楚,照抄先。

3.next()

讀取字符串,要讀取到有效字符后才結束輸入,不能讀取空格,即遇到空格就停。

        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");

輸入:123 456 789 1011

輸出:

123
456
789
Yes

s1第一次讀取到有效字符1,遇到4前的空格結束第一次輸入;隨后s2遇到第一個有效字符4,遇到空格結束;s3遇到第一個有效字符7,同理遇到空格結束,此時還有未讀取的字符在緩沖區里,用hasNext()判斷;

4.hasNext()

判斷是否還有輸入的數據,不能識別空格或回車,還會吃掉空格或者回車,連續的空格加回車一次性全部吸收掉,ACM里用循環讀取下一組數據。

5.nextLine()

和next()類似,唯一不同就是,next()遇到空格或者回車就斷了,nextLine()遇到回車才斷,空格也算是有效字符,從第一個有效字符開始直到回車,中間無論多少空格都能吃下。

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");
        
    }
}
/**輸入:123 456 789 1011 12 13 14 15
輸出:
123
456
789
 1011 12 13 14 15
s4把9后面的全部字符全部都吃下去了,沒有未讀取的字符
*/
nextLine()

6.hasNextLine()

可以判斷空格和回車,但是不會吃掉任何字符。

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();
    }
}
測試hasNext()會吃掉空格,hasNextLine()不會

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();
    }
}
測試hasNext()會吃掉空格,hasNextLine()不會

  • 通過比較可以知道hasNextLine()可以判斷空格和回車,並且不會吸收字符;
  • 但是如果先遇到hasNext(),空格加回車全都被吃掉,后續接上hasNextLine()判斷不到還有空格和回車。

7.其他類型的輸入

nextDouble();

nextLong();

nextInt();

nextFloat();

nextByte();

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM