Convert Enum to Int in Java

Sheeraz Gul Sep 08, 2022
Convert Enum to Int in Java

This tutorial demonstrates how to convert Enum to Int in Java.

Convert Enum to Int in Java

Java doesn’t provide built-in functionality to convert enum to int or vice versa, but we can create methods to perform these operations. We can convert enum with int values to int using the value() method.

Let’s see the step-by-step process:

  1. First of all, generate your Enum with int values. In our case:

    enum Cars {
    	Toyota(10),
    	Mercedes(20),
    	Tesla(30),
    	BMW(40),
    	Audi(50);
    }
    
  2. Create a method to get the enum Cars values.

    public int getCarAsInt() {
    		return cars;
    }
    
  3. Create a method to convert the Cars enum member to int. This method will include a for loop and an if conditional statement, which returns the integer value.

    public static int convertCarToInt(Cars inputCar) {
    		for (Cars cars : Cars.values()) {
    			if (cars.getCarAsInt() == inputCar.getCarAsInt()) {
    				return cars.getCarAsInt();
    			}
    		}
    		return -1;
    }
    
  4. We can also create a similar method to convert the int to enum Cars.

    public static Cars convertIntToCar(int intCars) {
    		for (Cars cars : Cars.values()) {
    			if (cars.getCarAsInt() == intCars) {
    				return cars;
    			}
    		}
    		return null;
    	}
    
  5. Finally, apply the above methods to each enum member. Let’s try an example and apply each method to each member of the enum Cars, which will convert the enum members to int and int back to the enum member:

    package delftstack;
    enum Cars {
    	Toyota(10),
    	Mercedes(20),
    	Tesla(30),
    	BMW(40),
    	Audi(50);
    	private final int cars;
    	Cars(int cars) {
    		this.cars = cars;
    	}
    	public int getCarAsInt() {
    		return cars;
    	}
    	public static int convertCarToInt(Cars inputCar) {
    		for (Cars cars : Cars.values()) {
    			if (cars.getCarAsInt() == inputCar.getCarAsInt()) {
    				return cars.getCarAsInt();
    			}
    		}
    		return -1;
    	}
    	public static Cars convertIntToCar(int intCars) {
    		for (Cars cars : Cars.values()) {
    			if (cars.getCarAsInt() == intCars) {
    				return cars;
    			}
    		}
    		return null;
    	}
    }
    public class Example {
    	public static void main(String[] args) {
    		// For enum member Toyota
    		Cars Toyota_Car = Cars.Toyota;
    		int intCars = Toyota_Car.getCarAsInt();
    		// Get car as integer value
    		System.out.println("Get Toyota car as int value :" + intCars);
    		Toyota_Car = Cars.convertIntToCar(10);
    		// Convert integer value to corresponding car value
    		System.out.println("Convert integer 10 to car enum :" + Toyota_Car);
    		intCars = Cars.convertCarToInt(Cars.Toyota);
    		// Convert Cars value to corresponding integer value
    		System.out.println("Convert Toyota car to integer :" + intCars + "\n");
    		// For enum member Mercedes
    		Cars Mercedes_Car = Cars.Mercedes;
    		intCars = Mercedes_Car.getCarAsInt();
    		// Get car as integer value
    		System.out.println("Get Mercedes car as int value :" + intCars);
    		Mercedes_Car = Cars.convertIntToCar(20);
    		// Convert integer value to corresponding car value
    		System.out.println("Convert integer 10 to car enum :" + Mercedes_Car);
    		intCars = Cars.convertCarToInt(Cars.Mercedes);
    		// Convert Cars value to corresponding integer value
    		System.out.println("Convert Mercedes car to integer :" + intCars+ "\n");
    		// For enum member Tesla
    		Cars Tesla_Car = Cars.Tesla;
    		intCars = Tesla_Car.getCarAsInt();
    		// Get car as integer value
    		System.out.println("Get Tesla car as int value :" + intCars);
    		Tesla_Car = Cars.convertIntToCar(30);
    		// Convert integer value to corresponding car value
    		System.out.println("Convert integer 10 to car enum :" + Tesla_Car);
    		intCars = Cars.convertCarToInt(Cars.Tesla);
    		// Convert Cars value to corresponding integer value
    		System.out.println("Convert Tesla car to integer :" + intCars+ "\n");
    		// For enum member BMW
    		Cars BMW_Car = Cars.BMW;
    		intCars = BMW_Car.getCarAsInt();
    		// Get car as integer value
    		System.out.println("Get BMW car as int value :" + intCars);
    		BMW_Car = Cars.convertIntToCar(40);
    		// Convert integer value to corresponding car value
    		System.out.println("Convert integer 10 to car enum :" + BMW_Car);
    		intCars = Cars.convertCarToInt(Cars.BMW);
    		// Convert Cars value to corresponding integer value
    		System.out.println("Convert BMW car to integer :" + intCars+ "\n");
    		// For enum member Audi
    		Cars Audi_Car = Cars.Audi;
    		intCars = Audi_Car.getCarAsInt();
    		// Get car as integer value
    		System.out.println("Get Audi car as int value :" + intCars);
    		Audi_Car = Cars.convertIntToCar(50);
    		// Convert integer value to corresponding car value
    		System.out.println("Convert integer 10 to car enum :" + Audi_Car);
    		intCars = Cars.convertCarToInt(Cars.Audi);
    		// Convert Cars value to corresponding integer value
    		System.out.println("Convert Audi car to integer :" + intCars+ "\n");
    	}
    }
    

    The code above will convert each enum Cars member to int and vice versa. See the output:

    Get Toyota car as int value :10
    Convert integer 10 to car enum :Toyota
    Convert Toyota car to integer :10
    
    Get Mercedes car as int value :20
    Convert integer 10 to car enum :Mercedes
    Convert Mercedes car to integer :20
    
    Get Tesla car as int value :30
    Convert integer 10 to car enum :Tesla
    Convert Tesla car to integer :30
    
    Get BMW car as int value :40
    Convert integer 10 to car enum :BMW
    Convert BMW car to integer :40
    
    Get Audi car as int value :50
    Convert integer 10 to car enum :Audi
    Convert Audi car to integer :50
    
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Enum