Question: Hello! My code works for the most part, but my catch statement isn't printing. It just goes back to the main menu. How do I

Hello! My code works for the most part, but my catch statement isn't printing. It just goes back to the main menu. How do I fix this? Java only please and thank you!
System.out.println("Please enter in the amount of students you would like to enroll: ");
Scanner sc = new Scanner(System.in);
int newEnrollment = sc.nextInt();
sc.nextLine();
for (int i =0; i < newEnrollment; i++){
System.out.println("Student"+(i+1)+" information:");
System.out.println("First Name: ");
String first = sc.nextLine();
System.out.println("Last Name: ");
String last = sc.nextLine();
System.out.println("Age: ");
try {
int age = sc.nextInt();
if(age >=4 && age <=18){
StudentInfo newStudent = new StudentInfo(first, last, age);
student.add(newStudent);
System.out.println("");
displayStudentInfo(student);
System.out.println("");
}
}catch(NumberFormatException e){
System.out.println("Student does not meet age requirements");
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!