This is my simple Java program:

import java.util.*;

public class Person{
public static void main(String[] args){
Scanner sc = new Scanner(;
String name;
int age;

System.out.println("Enter a person's name: ");
name =;
System.out.println("Enter a person's age: ");
age = sc.nextInt();

System.out.println("Name: " + name + "\nAge: " + age);


Now, after I enter an integer for the age I get an InputMismatchException and it says the error is on Line 13 [ age=sc.nextInt(); ]
Can someone explain to me why this happens and how do I correct it?

I've tried changing the data type of age to String and it works just fine.
I also tried removing the sc.useDelimiter("\r") line and it works fine but of course I cannot enter names like "Jane Smith". Does this mean the Scanner cannot recognize the integer I entered or took the "\r" as my data?

By the way, I'm pretty sure the problem is with the Scanner class but if I'm wrong, please correct me.