InputMismatchException while inputting integer after a string

  Kiến thức lập trình

I am trying to revise java after 2 years and am stuck in this error while taking input of integer after a string.
Here’s the code (yob is year of birth):

import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner inputter = new Scanner(System.in);
        String name, email;
        int yob;
        
        User users[] = new User[5];
        for(int i = 0; i < 5; i++) {
            
            System.out.print("Enter your name: ");
            name = inputter.next();
            inputter.reset();
            System.out.print("Which year were you born in? ");
            yob = inputter.nextInt();
            System.out.print("What's your email address? ");
            email = inputter.nextLine();
            users[i] = new User(name, yob, email);
            // To keep spacing between two sets of user input
            System.out.println(); 
            System.out.println();
        }
        
        System.out.println(users);
        inputter.close();

    }
}

And there’s the error:

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT