Convert string to Enum class

Disclaimer: I will not say that I am the most experienced Java person. My examples may have simpler ways of doing what I have. But it's a matter of converting a string to an Enum class, not an enum variable. I tried to explain as best as possible.

This is a simple version of what I want to do. I will have two different enumerations, and I want to choose one or the other, proceeding from what the user wants. And an example of this, in code, would be

public enum Letters { A, B, C, D, E, ... , X, Y, Z } public enum Numbers { ONE, TWO, THREE, ..., EIGHT, NINE, TEN } public static void main(String [] args) { System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)"); String entry = ""; InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); try { entry = reader.readLine(); } catch(Exception e){}; ... } 

I want to make it possible for the user, if he enters the "Letters", be able to take a string entry and turn it into a variable link to the "List of letters". Ellipses are holders of code space. I want to end up using this to use

 for (answer n : variable.values()) { System.out.println(n); } 

instead

 if (answer.equals("Numbers")) { Numbers n; for (n : Numbers.values()) { System.out.println(n); } } else { Letters l; for (l : Letters.values()) { System.out.println(l); } } 

I tried to do

 Class<?> c = Class.forName("Letters"); 

but that will not work.

In my example in the real world, I have 3.java files that contain separate classes, and an enumeration in each of them. There is a class of sandwich, beer and chip, and their transfers are sandwiches, beer, chips. These classes implement the VendingMachineItem interface. Finally, this interface is part of the vendingMachine package. I do not know if it matters, but all that I can give.

+6
source share
4 answers

The following works (tested with Java6 in Eclipse):

 package com.bob; public class Test { public enum Letters { A, B, C, D, E } public enum Numbers { ONE, TWO, THREE, FOUR, FIVE } public static void main(String [] args) throws Exception { System.out.println("Enter in you choice, letters (Letters) or numbers(Numbers)"); String entry = ""; InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); entry = reader.readLine(); Class<Enum<?>> clazz = (Class<Enum<?>>)Class.forName("com.bob.Test$" + entry); for (Enum<?> x : clazz.getEnumConstants()) { System.out.println(x); } } } 

Note that the fully qualified class name of the com.bob.Test$Letters and com.bob.Test$Numbers is associated with inner classes. Error handling remains as an exercise.

+2
source

I'm not sure if I called you completely. But do you want to do something like the following?

  package com.test; enum EnumType {A1, A2, A3}; public class Test { public static void main(String[] args) throws ClassNotFoundException { Class<?> clazz = Class.forName("com.test.EnumType"); if (!clazz.isEnum()) { throw new IllegalArgumentException("Enum type expected"); } Enum<?>[] constants = (Enum<?>[])clazz.getEnumConstants(); for (Enum<?> constant : constants) { System.out.println(constant.name()); } } } 
+1
source

You want to use valueOf() . If the selection is letters, you should use Letters.valueOf(input) , and if the selection is a number, you should use Numbers.valueOf(input) .

+1
source

Is this what you need (using Java 6 syntax)?

 String firstInput = // "Letters"; String secondInput = // "A" Letters letter = null; Numbers number = null; if (Letters.class.getSimpleName().equals(input)) { letter = Enum.valueOf(Letters.class, secondInput); } else if (Numbers.class.getSimpleName().equals(input)) { number = Enum.valueOf(Numbers.class, secondInput); } 

If the entries above the letter are equal to A and number remains null when the code executes.

0
source

Source: https://habr.com/ru/post/901751/


All Articles