0% found this document useful (0 votes)
9 views24 pages

Enums User Input

The document provides an overview of enums in Java, explaining their definition, usage, and how to implement them within classes and switch statements. It also covers the Scanner class for user input, detailing various methods to read different data types. Additionally, it includes exercises and an activity for creating an Employee Management System that utilizes these concepts.

Uploaded by

schweinekiroyama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Enums User Input

The document provides an overview of enums in Java, explaining their definition, usage, and how to implement them within classes and switch statements. It also covers the Scanner class for user input, detailing various methods to read different data types. Additionally, it includes exercises and an activity for creating an Employee Management System that utilizes these concepts.

Uploaded by

schweinekiroyama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Enums

}
{
An enum is a special "class" that represents a
group of constants (unchangeable
variables, like final variables).
To create an enum, use the enum keyword
(instead of class or interface), and separate
the constants with a comma.

Note that they should be in uppercase


letters: }
Example

enum Level { You can access enum constants with a


LOW,
MEDIUM, “dot” syntax.
HIGH
} Level myVar = Level.MEDIUM;

In the code above, we created an enum called Level.


Enum is short for “enumerations” which
You may notice that the values of this enum are all means “specifically listed”.
written in uppercase – this is just a general
convention. You will not get an error if the values are
lowercase.
Enum inside a Class
You can also have an enum inside a class:
public class Main {
enum Level {
LOW,
MEDIUM,

}
HIGH The output will be?
public static void main(String[] args) {
Level myVar = Level.MEDIUM;
System.out.println(myVar);
}
}
Enum in a Switch Statement
enum Level {
LOW,
MEDIUM,
HIGH Enums are often
}
used
public class Main {
public static void main(String[] args) { in switch statemen
Level myVar = Level.MEDIUM;
ts to check for
switch(myVar) {
case LOW: corresponding
System.out.println("Low level");
break;
values:
case MEDIUM:
System.out.println("Medium level");
break;
case HIGH:
System.out.println("High level");
break;
}
}
}
Loop Through an Enum
The enum type has a values() method, which returns an array of all enum
constants. This method is useful when you want to loop through the
constants of an enum:

enum Level {
LOW,
MEDIUM,
HIGH,
}
public class Main {
public static void main(String[] args) {
for (Level myVar : Level.values()) {
System.out.println(myVar);
}
}
}
Difference between Enums and
Classes

An enum can, just like a class, have attributes and


methods. The only difference is that enum constants
are public, static and final (unchangeable -
cannot be overridden).

An enum cannot be used to create objects, and it


cannot extend other classes (but it can implement
interfaces).
// Define the interface // Usage
interface Actionable { public class Main {
void performAction(); } public static void main(String[] args) {

// Define the enum implementing the interface for (Task task : Task.values()) {
enum Task implements Actionable { System.out.print(task.name() + ": ");
task.performAction();
CLEAN { }
public void performAction() { }
System.out.println("Cleaning in progress..."); }
}
}, Explanation:
1.Interface Definition: The Actionable interface is created with
WASH { the method performAction.
public void performAction() { 2.Enum Implementation: The Task enum implements the
System.out.println("Washing in progress..."); Actionable interface. Each constant provides its specific
} implementation for the performAction method.
}, 3.Dynamic Behavior: When performAction is called on a Task
COOK { constant, the respective implementation is executed.
public void performAction() {
System.out.println("Cooking in progress..."); Output of the Example:
}
} CLEAN: Cleaning in progress...
} WASH: Washing in progress...
COOK: Cooking in progress...
Why And When To Use Enums?

Use enums when you have values that you know


aren't going to change, like month days, days,
colors, deck of cards, etc.
Exercise

How can you access enum


constants?
What is an enum?
 A special class that
represents a group of
constants
Consider the following code:

enum Level {
LOW,
MEDIUM,
HIGH }

Fill in the missing part below to loop through the constants


of the Level enum:

for (Level myVar : Level. ) {


System.out.println(myVar);
}
Java User Input (Scanner)
import java.util.Scanner; // Import the Scanner class

Java User Input class Main {


public static void main(String[] args) {
The Scanner class is used to get
user input. Scanner myObj = new Scanner(System.in); // Create a
To use the Scanner class, create Scanner object
an object of the class and use any System.out.println("Enter username");
of the available methods found in
the Scanner class String userName = myObj.nextLine(); // Read user
documentation. In our example, input
we will use
System.out.println("Username is: " + userName); //
the nextLine() method, which is
used to read Strings:
Output user input
}
}
Input Types
In the previous example, we used the nextLine() method, which is
used to read Strings. To read other types, look at the table below:
Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user
Integer Input from the User

The nextInt() method is used to


take input of an integer from the
user.
//Example
Importing the class
import java.util.Scanner;
In the following example, we are taking an integer as an input −

public class IntegerInput {


public static void main(String[] args) {

// Creating an object of Scanner class


Scanner sc = new Scanner(System.in);

// Reading an Integer Input


System.out.print("Input an integer value: ");
int int_num = sc.nextInt();

System.out.print("The input is : " + int_num);


}
}
In the example below, we use different methods to read data of
various types:
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);

System.out.println("Enter name, age and salary:");

// String input
String name = myObj.nextLine();

// Numerical input
int age = myObj.nextInt();
double salary = myObj.nextDouble();

// Output input by user


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}
Note: If you enter wrong input (e.g. text in a
numerical input), you will get an
exception/error message (like
"InputMismatchException").
Exercise

What is a correct syntax to create a Scanner


object?

Class Scan = new Scanner(System.in);


Scanner myObj = new
Scanner(System.in);
Main myObj = new Main(Scanner);
Scan myObj = new Scan(Scanner);
Fill in the missing parts to import
the java.util.Scanner class from the Java API:

. . . ;
Which method is used to read Strings from
input?
The nextLine() method
The Scanner() method
The readString() method
The userInput() method
Which method is used to read integers from
input?
The nextLine() method
The integer() method
The nextInt() method
The readInt() method
Activity: Employee Management System
In this activity, users will be able to enter different types of information about an
employee: name (String), age (int), salary (double), and employee status (boolean). Then,
the program will display the details of the employee.
Output:
Objective:
•Input Types: String, int, double, boolean. Enter employee name: John Doe
•Output: Display the entered information. Enter employee age: 30
Enter employee salary: 55000.75
Step-by-Step Instructions:
Is the employee active?
1.Create a new Java class called
EmployeeManagementSystem.
(true/false): true
2.Use the following Java input types:
• String for employee name. Employee Details:
• int for employee age. Name: John Doe
• double for employee salary. Age: 30
• boolean for employee status (active or Salary: $55000.75
inactive). Active: Yes

You might also like