Java Program to Check Voting Eligibility

In this tutorial, we will learn how to write a Java program to check voting eligibility. We will explore different ways to check whether a person is eligible to vote or not.

Before writing the Java program, let us understand the basic condition for voting eligibility.

What Is Voting Eligibility?

Voting eligibility refers to whether a person meets the requirements to cast a vote in an election. The basic rules to be eligible for a vote are:

  • Eligible to Vote: If the entered age is 18 or above, the program displays a message, “You are eligible to vote.”.
  • Not Eligible Yet: If the entered age is below 18, the program displays a message, “You are not eligible to vote.”.
  • Invalid Age: If the entered age is negative, the program displays an invalid-age message.

Java Program to Check Voting Eligibility Using if-else

Let us first write a simple Java program in which we will use the if-else statement to check whether a person is eligible to vote. In this approach, we will assign the age value directly to a variable named age.

Complete Java Program

// Java program to check voting eligibility using if-else
public class VotingEligibilityCheck {
    public static void main(String[] args) {
        // Declaring and initializing the age variable
        int age = 20;

        // Checking eligibility using if-else
        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        } else {
            System.out.println("You are not eligible to vote.");
        }
    }
}

Expected Output:

You are eligible to vote.

In this example program:

  • We declare an integer variable named age and assign it the value 20.
  • The if statement checks whether the age is greater than or equal to 18.
  • If the condition is true, the program displays the message, “You are eligible to vote.”.
  • Otherwise, the else block displays a message, “You are not eligible to vote.”.

Since the initialized age is 20, the condition age >= 18 evaluates to true. Therefore, the program displays a message, “You are eligible to vote.”.

This example uses a fixed age. To check the eligibility of different people, we can accept the age from the user’s keyboard using the Scanner class.

Java Program to Check Voting Eligibility Using Scanner

In the previous example, we assigned the age directly to a variable. However, we often need to accept input from users in real-world applications.

To achieve this, Java provides the Scanner class in the java.util package to read input from the keyboard. Let us write a program that accepts a person’s age from the user’s keyboard and checks voting eligibility.

Complete Java Program

// Java program to check whether a person is eligible to vote using Scanner.
import java.util.Scanner;
public class VotingEligibilityScanner {
    public static void main(String[] args) {

        // Create a Scanner object to read input from the keyboard
        Scanner sc = new Scanner(System.in);

        // Prompt the user and accept the age from the console
        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        // Check whether the age is valid or eligible using an if-else-if ladder
        // Condition 1: Handle invalid negative ages
        if (age < 0) {  
             System.out.println("Invalid age entered."); 
        } else if (age >= 18) {
            // Condition 2: Check if the user meets the voting age requirement
            System.out.println("You are eligible to vote.");
        } else {
            // Condition 3: Handle ages below 18
            System.out.println("You are not eligible to vote.");
        }

        // Close the Scanner object to prevent resource leaks
        sc.close();
    }
}

Expected Output:

Enter your age: 16
You are not eligible to vote.

Let us understand the important statements used in this Java program.

  • The statement import java.util.Scanner; imports the Scanner class from the java.util package.
  • Scanner sc = new Scanner(System.in); creates a Scanner object to read the input from the user’s keyboard.
  • int age = sc.nextInt(); reads an integer entered by the user and stores it in the age variable.
  • if (age < 0) checks whether the entered age is negative.
  • else if (age >= 18) checks whether the person is at least 18 years old.
  • else executes when the age is between 0 and 17.
  • sc.close(); closes the scanner after the input has been processed.

The program first checks whether the age is valid. It then determines voting eligibility using the if-else statement.

Java Program to Check Voting Eligibility Using Nested if-else

We can also check voting eligibility in Java using nested if-else statements. In this approach, one if-else statement is placed inside another conditional block. Let’s write a Java program to check whether a person is eligible to vote or not using nested if-else statements.

Complete Java Program

// Java program to check voting eligibility using nested if-else
import java.util.Scanner;
public class VotingEligibilityNested {
    public static void main(String[] args) {

        // Create a Scanner object to read input from the keyboard
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter their age
        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        // Outer if-else block: Validate if the entered age is a positive number (>= 0)
        if (age >= 0) {

            // Inner if-else block: Check voting eligibility once the age is confirmed valid
            if (age >= 18) {
                System.out.println("You are eligible to vote.");
            } else {
                System.out.println("You are not eligible to vote.");
            }

        } else {
            // Executed if the outer condition fails (age is negative)
            System.out.println("Invalid age entered.");
        }

        // Close the Scanner object to prevent resource leaks
        sc.close();
    }
}

Expected Output:

Enter your age: 17
You are not eligible to vote.

In this program code:

  • The outer if statement checks whether the age is non-negative.
  • If the age is valid, the inner if-else statement checks whether the person is at least 18 years old.
  • If the age is 18 or above, the program displays the eligible message.
  • If the age is below 18, the program displays the ineligible message.
  • If the age is negative, the outer else block displays an invalid-age message.

Nested if-else statements are useful when one condition should be checked only after another condition has been satisfied. However, if your program is simple, you use a regular if-else statement for better understanding.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.