enum problem
I'm having a bit of a problem with my enum class.
The last method "public java.lang.String stringValue()"
- is suppose to obtain the string value (basic, simple, or standard) from the GameVariant.
I can't figure out my problem can someone help me out?
publicenum GameVariant
{
BASIC, SIMPLE, STANDARD;
publicstatic GameVariant fromString(final String name)
throws InvalidGameVariantFormatException
{
if(name ==null)
{
thrownew IllegalArgumentException("Name needs to have a value");
}
if(name == ("STANDARD"))
{
thrownew InvalidGameVariantFormatException("name must be either \"basic\", \"simple\" or \"standard\", was: \"STANDARD\"");
}
if(name == ("Basic"))
{
thrownew InvalidGameVariantFormatException("name must be either \"basic\", \"simple\" or \"standard\", was: \"Basic\"");
}
if(name == ("mauve"))
{
thrownew InvalidGameVariantFormatException("name must be either \"basic\", \"simple\" or \"standard\", was: \"mauve\"");
}
elseif(name.equals("basic"))
{
return GameVariant.BASIC;
}
elseif(name.equals("simple"))
{
return GameVariant.SIMPLE;
}
elseif(name.equals("standard"))
{
return GameVariant.STANDARD;
}
else
{
returnnull;
}
}
public java.lang.String stringValue()
{
return("basic, simple, standard");
}
}
[3457 byte] By [
vopoa] at [2007-11-27 3:44:27]

Why are you even writing that? Just use toString.
And for that matter, don't write fromString. Use valueOf.
GameVariant.valueOf() and GameVariant.toString() will be automatically produced by the compiler (or via inheritance) when you create a enum. You don't need to create anything more. Yeah, the documentation isn't great.
I can't change the fromString method, my instructor told me to use the code.
vopoa at 2007-7-12 8:48:07 >

> I can't change the fromString method, my instructor
> told me to use the code.
Have some real fun, be brave, do it the right way and when your instructor gripes, point out that their code didn't follow standard Sun Java convention and watch them immediately start to backpedal.
PS.
But is there a way to fix my last method, I'm trying to obtain the string value (basic, simple, or standard) from the GameVariant?I want to call each one individually, is there a possible way to do it?
vopoa at 2007-7-12 8:48:07 >

The language reference has a good example of going back-and-forth between strings and enums.
import java.util.HashMap;
import java.util.Map;
public class genum {
enum GameVariant {
BASIC, SIMPLE, STANDARD;
static final Map<String,GameVariant> gvMap =
new HashMap<String,GameVariant>();
static {
for (GameVariant gv : GameVariant.values()) {
gvMap.put(gv.toString(), gv);
}
}
public static boolean isValidGV(String _gv) {
if( _gv == null ) return false;
return (gvMap.get(_gv.toUpperCase()) != null);
}
}
public static void main(String[] args) {
System.out.println( "isValid 'soda'?: " +
GameVariant.isValidGV("soda"));
System.out.println( "isValid 'basic'?" +
GameVariant.isValidGV("basic"));
System.out.println( "isValid 'BASIC'?" +
GameVariant.isValidGV("BASIC"));
System.out.println( "isValid 'null'?" +
GameVariant.isValidGV(null) );
}
}
> But is there a way to fix my last method, I'm trying
> to obtain the string value (basic, simple, or
> standard) from the GameVariant?
>
> I want to call each one individually, is there a
> possible way to do it?
Can you explain what you're trying to do, which isn't already solved by toString or valueOf?
public class EnumTest {
enum Animal {
octopus, horse, monkey, zebra, elephant;
}
public static void main(String[] argv) {
Animal a = Animal.valueOf("monkey");
System.out.println(a); // prints "monkey"
// if you want more detail about above line:
String animalName = a.toString();
if ("monkey".equals(animalName)) {
System.out.println("same name"); // this is printed
} else {
System.out.println("not same name"); // this is not printed
}
}
}
> I can't change the fromString method, my instructor
> told me to use the code.
One of the following is true:
1) you misunderstood your instructor
2) your instructor is hoping that you'll figure out what these basic enum methods are, and that your code will invoke those methods, or you'll just use those methods directly without writing any wrappers, or
3) your instructor is an idiot
Oh, and in case you're dealing with #3:
public String methodThatDoesTheSameThingAsToString() {
return toString();
}
or perhaps
public String methodThatDoesTheSameThingAsToStringExceptItMightChangeCase() {
return toString().toLowerCase();
}
Don't compare String value with == use equals() or (in this case) equalsIgnoreCase()
Even if you can't use valueOf, if you have a string value associated with a each enum case then search with a loop, rather than an if- else if chain.
You can add any field you like to an enum, and give a contructor which sets those fields.