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.