Java ProgramsControl FlowCheck Driving Eligibility

Check Driving Eligibility in Java

intermediate·  Control Flow  ·  Conditional Statements

Problem

Driving eligibility depends on both the vehicle category and a minimum age tied to that category, so the check has to combine two pieces of information instead of comparing a single value.

Given a vehicle type and a person's age, determine whether they meet the minimum age required to drive that type of vehicle.

Input
vehicleType = "car", age = 19
Output
Eligible to drive car: true

Java Program

Java
public class DrivingEligibility { public static void main(String[] args) { String vehicleType = "car"; int age = 19; boolean eligible; if (vehicleType.equals("two-wheeler without gear")) { // equals(), not ==, for String content comparison eligible = age >= 16; } else if (vehicleType.equals("car") || vehicleType.equals("two-wheeler with gear")) { eligible = age >= 18; } else if (vehicleType.equals("commercial vehicle")) { eligible = age >= 20; } else { eligible = false; // unrecognized vehicle type } System.out.println("Eligible to drive " + vehicleType + ": " + eligible); } }

Output

Eligible to drive car: true

Core Logic

Matching the vehicle type against each category in turn, and comparing the age against that category's minimum, covers every case with a single chain of checks.

How It Works
  1. 1vehicleType.equals(...) is used instead of ==, since string content — not object identity — is what matters here.
  2. 2"two-wheeler without gear" requires only age >= 16, the lowest threshold in this scheme.
  3. 3"car" and "two-wheeler with gear" are grouped under the same age >= 18 check using ||, since they share a minimum age.
  4. 4"commercial vehicle" requires age >= 20, and any unrecognized vehicle type falls through to false.
For vehicleType = "car" and age = 19, the second branch matches and 19 >= 18 evaluates to true.
💡

Key Point: This illustrative age scheme is for demonstrating multi-branch conditionals, not a statement of any real jurisdiction's actual licensing law.

Key Concepts

if / else ifString.equals()logical OR

Approach 2: Using Switch Statement

Java
public class DrivingEligibilitySwitch { public static void main(String[] args) { String vehicleType = "car"; int age = 19; int minimumAge; switch (vehicleType) { case "two-wheeler without gear": minimumAge = 16; break; case "car": case "two-wheeler with gear": // stacked labels fall through to the same assignment minimumAge = 18; break; case "commercial vehicle": minimumAge = 20; break; default: minimumAge = Integer.MAX_VALUE; // unknown vehicle type is never eligible } boolean eligible = age >= minimumAge; System.out.println("Eligible to drive " + vehicleType + ": " + eligible); } }

Output

Eligible to drive car: true

Core Logic

A switch statement can look up each vehicle type's minimum age directly, separating 'what age is required' from 'does this person meet it'.

How It Works
  1. 1switch (vehicleType) matches the string against each case label in turn.
  2. 2"car" and "two-wheeler with gear" share a case block by stacking their labels together, so both fall through to the same minimumAge = 18 assignment.
  3. 3The default case assigns Integer.MAX_VALUE as the minimum age, guaranteeing an unrecognized vehicle type is never treated as eligible.
  4. 4Once minimumAge is resolved, a single comparison — age >= minimumAge — determines eligibility, the same as the if/else version's final check.
For vehicleType = "car", the switch resolves minimumAge to 18, and 19 >= 18 evaluates to true.
💡

Key Point: Stacking case labels without a break between them is what lets "car" and "two-wheeler with gear" share one assignment — separating the lookup from the comparison keeps each part of the logic simple.

Key Concepts

switch statementfall-through casesdefault case

Related Programs