R
Rishtaara
Java Fundamentals
Lesson 23 of 36Article18 min

Enum, User Input & Date

enum defines named constants: enum Day { MON, TUE, WED }. Type-safe alternative to magic strings.

Enum — fixed set of constants

enum defines named constants: enum Day { MON, TUE, WED }. Type-safe alternative to magic strings.

Use in switch, compare with ==.

Real-life example: Enum is a menu with only 3 combo options — you cannot order "combo 99" if it is not on the list.

Enum example
enum Status {
    PENDING, APPROVED, REJECTED
}

Status s = Status.PENDING;
if (s == Status.APPROVED) {
    System.out.println("Done");
}

User Input — Scanner

Scanner reads keyboard input. import java.util.Scanner; wrap System.in.

Methods: nextLine() for full line, nextInt() for integer, nextDouble() for decimal.

Real-life example: Scanner is a waiter taking your order — you type, program reads and remembers.

Scanner input
import java.util.Scanner;

Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.println("Hi " + name + ", age " + age);
sc.close();

Date — java.time (modern API)

Prefer java.time over old Date. LocalDate for date, LocalDateTime for date+time.

Use DateTimeFormatter for formatting strings.

Real-life example: LocalDate is a calendar page (2026-07-24). LocalDateTime adds the clock time (14:30).

LocalDate and formatting
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate today = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MM-yyyy");
System.out.println(today.format(fmt));