-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerLoop.java
More file actions
39 lines (34 loc) · 1.08 KB
/
Copy pathScannerLoop.java
File metadata and controls
39 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package LearningPackage;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ScannerLoop {
static void checkAge(int age){
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
} else {
System.out.println("Access granted - You are old enough!");
}
System.out.println("Still alive!");
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int age;
do {
try {
System.out.println("Enter your age:");
age=sc.nextInt();
checkAge(age);
break;
}
catch (InputMismatchException e) {
System.out.println(e.fillInStackTrace());
System.out.println("enter your age as a whole number");
sc.next();
}
catch (Exception e) {
System.out.println(e.fillInStackTrace());
}
} while (true);
}
}