Disclaimer: I am not going to say that I am the most experienced Java person. There could be easier ways to do what I have in my examples. But this is a question about turning a string entry into an Enum class, not an enum variable. I tried to explain as best as I could.
This is a simple version of what I want to do. I will have two different enums and I want to choose one or the other based off what the user wants. And 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 so the user, if he enters in "Letters" it will be able to take the string entry and turn it into a variable reference to the Letters Enum. The ellipses are place holders for code. I want to eventually use this so I can use
for (answer n : variable.values()) {
System.out.println(n);
}
instead of
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 have tried doing
Class<?> c = Class.forName("Letters");
but it doesn't work.
In my real world example, I have 3 .java files which contains separate classes, and an enum in each of them. There is Sandwich, Beer, and Chip class and their enums are Sandwiches, Beers, Chips. Those classes implement an interface VendingMachineItem. And finally, that interface is part of a vendingMachine package. I don't know if those matter, but that's all the info that I can give.
The following works (tested with Java6 under 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);
}
}
}
Notice that the fully qualified classname of the enums are com.bob.Test$Letters
and com.bob.Test$Numbers
due to being inner classes. Error handling is left as an exercise.
I'm not sure if I got you completely right. 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());
}
}
}
You want to use valueOf()
. If the choice is letters then you would use Letters.valueOf(input)
and if the choice is numbers you would use Numbers.valueOf(input)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With