谈谈Scanner类的next()和hasNext()方法

1、hasNext()与next()

在默认情况下,Scanner使用空格和回车作为多个输入项之间的分隔符

  • hasNext():判断是否还有下一个输入项。
  • next():获取下一个输入项。

对比下面的几个示例:

1、示例1
public class ScannerDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以空格分开
        while(sc.hasNext()) {
            String s1 = sc.next();
            System.out.println("s1="+s1);
        }
    }
}

在这里插入图片描述

2、示例2
public class ScannerDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以空格分开
        while(sc.hasNext()) {
            String s1 = sc.next();//获取第1个输入项
            String s2 = sc.next();//获取第2个输入项
            System.out.println("s1="+s1);
            System.out.println("s2="+s2);
        }
    }
}

在这里插入图片描述

3、示例3
public class ScannerDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以空格分开
        while(sc.hasNext()) {
            String s1 = sc.next();//获取第1个输入项
            String s2 = sc.next();//获取第2个输入项
            String s3 = sc.next();//获取第3个输入项
            System.out.println("s1="+s1);
            System.out.println("s2="+s2);
            System.out.println("s3="+s3);
        }
    }
}

在这里插入图片描述

4、示例4
public class ScannerDemo4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以空格分开
        while(sc.hasNext()) {
            String s1 = sc.next();
            String s2 = sc.next();
            System.out.println("s1="+s1);
            System.out.println("s2="+s2);
        }
    }
}

在这里插入图片描述

对上述代码的解析:
在这里插入图片描述

2、hasNextLine()与nextLine()

  • boolean hasNextLine():输入源中是否还有下一行。
  • String nextLine():获取输入源中的下一行的字符串。
    即只用回车作为输入项的分隔符,下一行就是一个输入项

示例1:只要按下回车键立马就会输出

public class ScannerDemo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以enter分开
        while(sc.hasNextLine()) {
            String s1 = sc.nextLine();
            System.out.println("s1="+s1);
        }
    }
}

在这里插入图片描述

示例2:按下第一个回车键enter并不会输出,按下第二个回车键输出,原因在于如果只有一行字符串,那么还没走到打印Sout就退出循环了

public class ScannerDemo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以enter分开
        while(sc.hasNextLine()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            System.out.println("s1="+s1);
            System.out.println("s2="+s2);
        }
    }
}

在这里插入图片描述
示例3:

public class ScannerDemo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以enter分开
        while(sc.hasNextLine()) {
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            System.out.println("s1="+s1);
            System.out.println("s2="+s2);
        }
    }
}

在这里插入图片描述

示例4:

public class ScannerDemo5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //判断sc中是否有下一个输入项,多个输入项以enter分开
        while(sc.hasNextLine()) {
            String s1 = sc.nextLine();
            System.out.println("s1="+s1);
        }
    }
}

在这里插入图片描述

原创文章 723 获赞 153 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_42764468/article/details/105461342