next:每次读取一个单词,以空格或者回车作为分隔符

nextLine:每次读取一行,以回车作为分隔符

package com.soldier.test;

import java.util.Scanner;

public class Exercise {

    public static void main(String[] args) {
        System.out.println("How old are you?");
        Scanner sc = new Scanner(System.in);
        // 输入: I am 23 years old
        String age = sc.next();
        System.out.println("age = " + age); // 输出:I

        Scanner ss = new Scanner(System.in);
        // 输入: I am 23 years old
        String age2 = ss.nextLine();
        System.out.println("age2 = " + age2); // 输出:I am 23 years old
    }
}

Scanner中next和nextLine的区别:_数据