When this runs, something goes wrong when the data changes from int to String. I think.
package people;
import java.util.Scanner;
public class People {
public static void main(String[] args) {
String name;
int age;
String college;
String petname;
Scanner scan = new Scanner (System.in);
System.out.println ("Enter your name:");
name = scan.nextLine();
System.out.println ("Enter your age:");
age = scan.nextInt();
System.out.println ("Enter the name of your college:");
college = scan.nextLine();
System.out.println ("Enter the name of your pet:");
petname = scan.nextLine();
System.out.println("Hello, my name is " + name + " and I am " + age + " years");
System.out.println("old. I'm enjoying my time at " + college + ", though");
System.out.println("I miss my pet " + petname + " very much!");
}
}
The console produces:
Enter your name: Tim Enter your age: 12 Enter the name of your college: Enter the name of your pet:
As you can see, the third and fourth scanner run together. If the input is completed, this is what happens:
Enter your name: Tim Enter your age: 12 Enter the name of your college: Enter the name of your pet: College Hello, my name is Tim and I am 12 years old. I'm enjoying my time at , though I miss my pet College very much!
There is no error when I set age to String, so I am very confused. Appreciate any explanations.