Question: Hello! My code works for the most part, I was wondering if it was possible to set up a try / catch to check if

Hello! My code works for the most part, I was wondering if it was possible to set up a try/catch to check if the age entered is within the valid range? 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: ");
int age = sc.nextInt();
sc.nextLine();
//valid age range : 4-18
}

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!