In Java, to check if a person is valid for voting or not we can use an if-else conditional statement or the ternary Operator.
For example, if the person age is 18 or older, then it should print that the “person is valid for voting”. If the person age is less than 18 then it should print the “person is not valid for voting”.
In this article, we will learn how to check if a person is valid or not for voting through an example, algorithm, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements(if-else and Switch)
- Ternary Operator
Problem Statement
We need to write a Java program to determine if a person is eligible to vote based on their age. The program should take an input value (an integer representing age) and print the result accordingly. Let’s look at an example:
Example 1:
Input: 13
User is not eligible to vote
Here, for the input 13, the output is “User is not eligible to vote” because the person’s age is 13, which is not valid for voting.
Example 2:
Input: 20
User is eligible to vote
Here, for the input 20, Output is “User is eligible to vote” as the age above 18 is eligible for voting.
Algorithm to check if a person valid for voting or not
- Start: Take an input value as age.
- Condition Check:
- if (age>=18)
- If age is 18 or greater, print “The person is valid for voting.”
- Otherwise, print “The person is not valid for voting.”
- End: The program will terminate.
Logic to check if a person is valid for voting or not
There are multiple logics to check if a person is valid for voting or not in Java Programing language.
Logic 1: Using if-else
- After taking an input value as age from the user, we need to check if the user is eligible to vote.
- If the user enters an age of 18 or above, the program will tell them they are eligible to vote. If the user enters an age below 18, it will print that the user is not eligible to vote. This is done using if-else statements.
if(age>=18)
System.out.println(age+" User is eligible to vote");
else
System.out.println(age+" User is not eligible to vote");
Logic 2: Using Ternary Operator
- The ternary operator takes three operands and works like if-else statements. If the condition is true, it prints the first expression. If the condition is false, it prints the second expression.
- Take an integer value as age from the user. Use the ternary operator to check if the age is 18 or more.
- If the age is 18 or more, store “User is eligible to vote” in the result variable. Otherwise, store “User is not eligible to vote” in the result variable.
String result;
result = (age >= 18) ? age + " User is eligible to vote" :
age + " User is not eligible to vote";
System.out.println(result);
Java Program to Check if a person is valid for voting or not
Program 1: Using if else
In the program below, we are using if else to check if a person is valid for voting or not in Java.
import java.util.Scanner;
public class ValidToVote {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter user age");
int age = s.nextInt();
if(age>=18)
System.out.println(age+" User is eligible to vote");
else
System.out.println(age+" User is not eligible to vote");
s.close();
}
}
Output:
Enter user age
13
13 User is not eligible to vote
Java program explanation to check if a person is valid for voting or not
- The Java program uses the
Scanner
class fromjava.util
to get user input. - It defines a class named
ValidToVote
and contains themain
method where execution begins. - The program asks the user to enter their age.
- It then checks if the entered age is 18 or higher. If the age is 18 or higher, it prints that the user is eligible to vote.
- If the age is below 18, it prints that the user is not eligible to vote.
- Finally, it closes the
Scanner
object to free up system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check if a person is valid for voting or not in Java.
import java.util.Scanner;
public class ValidToVote {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter user age");
int age = s.nextInt();
String result;
result = (age >= 18) ? age + " User is eligible to vote" :
age + " User is not eligible to vote";
System.out.println(result);
s.close();
}
}
Output:
Enter user age
18
18 User is eligible to vote
Java program explanation to check if a person is valid for voting or not
- The program imports the
Scanner
class fromjava.util
for user input. - It defines a class named
ValidToVote
and contains themain
method where execution starts. - The program asks the user to enter their age and uses a ternary operator to check if the age is 18 or above.
- If the age is 18 or above, it assigns a message indicating that the user is eligible to vote to the variable
result
. Otherwise, it assigns a message indicating that the user is not eligible to vote toresult
. - It then prints the message stored in
result
and closes theScanner
object to free system resources.
Conclusion
In this article, we explored how to check if a person is valid for voting or not using if-else statements and ternary operator. We understood the logic behind this and moved on to writing the program.
For more related information, you might find these articles helpful:
- Understanding the Conditional statements in Java
- Concepts of Operators in Java
- Top 10 programming tips in Java
We hope you find this article helpful. If you have any questions or need further clarification, please feel free to leave a comment below. Don’t forget to subscribe to our newsletter for more updates on Java programming tips and tutorials.
Happy Programming!