Person Eligibility (Age Check) in Java

Check if a person is eligible to vote based on age.

BeginnerModule 2: Conditional ProgramsExample 15 of 20
person-eligibility-age-check.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter age: ");
8 int age = sc.nextInt();
9
10 if (age >= 18) {
11 System.out.println("Eligible to vote");
12 } else {
13 System.out.println("Not eligible to vote");
14 }
15
16 sc.close();
17 }
18}

Output

Enter age: 20
Eligible to vote

What's going on

We compare age against the legal voting age threshold 18.