[Solved] Problems and solutions when using nextLine(), nextInt() and next() in Scanner

1. Questions

Code:

import java.util.ArrayList;
import java.util.Scanner;

public class JavaDemo {<!-- -->
    public static void main(String[] args) {<!-- -->
        // read from keyboard
        Scanner sc = new Scanner(System.in);
        //Create ArrayList collection object
        ArrayList<Student> students = new ArrayList<>();

        //Enter 5 student information in a loop and store it in the ArrayList collection
        for (int i = 0; i < 5; i + + ) {<!-- -->
            System.out.println("Please enter the first " + (i + 1) + " student name: ");
            String name = sc.nextLine();
            System.out.println("Please enter the first " + (i + 1) + " student gender: ");
            String gender = sc.nextLine();
            System.out.println("Please enter the age of " + (i + 1) + " students: ");
            int age = sc.nextInt();
            Student student = new Student(name, gender, age);
            students.add(student);
        }

        //Print
        for (Student s : students) {<!-- -->
            System.out.println(s);
        }
    }
}

The results are as follows:

As can be seen from the run results, entering the second student name was skipped.

The reason is because the ending methods of nextLine() and nextInt() are different, nextLine()method ends with Enter\
, nextInt()< strong>The method is it ends when it encounters a carriage return\
or a space, only reads the int type value entered before the carriage return, and continues to pass the following bytes.

This causes the nextLine() method to receive the carriage return\
after the input of nextInt() in the first loop, which is the “\
” in “23\
“, although the carriage return\
It is not displayed, but there is still transmission, so when entering the second student’s name, the program will read the carriage return\
to end, and proceed to the next input.

2. Solve with next()

To solve this problem, just replace nextLine() with next(), because next() ends with a space, and cannot read a carriage return\
or space, and when the next carriage return\
or space is encountered, it will will stop
.

Code:

import java.util.ArrayList;
import java.util.Scanner;

public class JavaDemo {<!-- -->
    public static void main(String[] args) {<!-- -->
        // read from keyboard
        Scanner sc = new Scanner(System.in);
        //Create ArrayList collection object
        ArrayList<Student> students = new ArrayList<>();

        //Enter 5 student information in a loop and store it in the ArrayList collection
        for (int i = 0; i < 5; i + + ) {<!-- -->
            System.out.println("Please enter the first " + (i + 1) + " student name: ");
            String name = sc.next();
            System.out.println("Please enter the first " + (i + 1) + " student gender: ");
            String gender = sc.next();
            System.out.println("Please enter the age of " + (i + 1) + " students: ");
            int age = sc.nextInt();
            Student student = new Student(name, gender, age);
            students.add(student);
        }

        //Print
        for (Student s : students) {<!-- -->
            System.out.println(s);
        }
    }
}

The results are as follows: