0

When parsing command line input using Scanner class in Java, there was an issue printing the last value. It can be fixed by changing the variable age to a String, but I would like to know the cause of the initial problem. The code is below.

import java.util.Scanner; 

public class Main {
    public static void main(String[] args) {
        
        // Create new instance of scanner
        
        Scanner scanner = new Scanner(System.in);
        
        // Ask for user details
        
        System.out.println("What is your first name?");
        String name = scanner.nextLine();
        
        System.out.println("What is your last name?");
        String surname = scanner.nextLine();
        
        System.out.println("How old are you?");
        int age = scanner.nextInt();
        
        System.out.println("Make a username.");
        String username = scanner.nextLine();
        
        System.out.println("What city do you live in?");
        String city = scanner.nextLine();
        
        System.out.println("What country is this city located in?\n");
        String country = scanner.nextLine();
        
        // print variable values
        
        System.out.println(name);
        System.out.println(surname);
        System.out.println(age);
        System.out.println(username);
        System.out.println(city);
        System.out.println(country);
        
        
    }
}

Thank you in advance.

0 Answers0