JeyesElite
JeyesElite

Reputation: 73

Java - How to use enums

I recently (<- keyword) got into programming Java, and I'm loving it! But I've ran into a little obstacle while trying to independently program... My idea was to make a fun little Character Creator - RPG Style, but I don't know how to get the user to select a class without using a bunch of If Statements.

I want to use enum's, even though I'm not really familiar with them.

So far I've got to the point of asking the user what class it wants to play + I've created the enum.

    String Name;

    System.out.println("Welcome to my Character Creator 2.0!\n");
    Thread.sleep(2000);
    System.out.print("First off, what do you want your characters name to be?\n\nName : ");
    Name = Scan.nextLine();
    Thread.sleep(1000);
    System.out.print("\nYou are now to be known as "+ Name + "!");

    System.out.print("\n\n" + Name + ", what class do you want to be? ");
    Thread.sleep(1000); 
        System.out.print("Classes available : Knight\nMage\nDruid\nNinja\nArcher\nAdventurer");
}

So yeah, I basically want to be able to call out the the classes right from the enum, but I'm unsure how to do it. I tried something stupid like : Class = Scan.nextInt();... but it obviously didn't work

Upvotes: 2

Views: 392

Answers (3)

Val
Val

Reputation: 11107

What you actually need is a map: string -> class. Using enums makes no sense (though it seems attractive, even to me) since it would introduce the redundancy (the evil), which you try to eliminate in your current approach. You'd better ask for the means to scan the available classes (a packege) and load classes dynamically, by name, instead of asking for enums. The redundancy and enums is something like

// mapping by enumerating the cases
name = Scanner.next()
case name 
  "knight": load (Druid.class)
  "mage": load (Mage.class)
  "ninja": load (Ninja.class)

Enjoy the direct approach: load(name). That simple! Just capture exception if no class corresponds to the name.

Upvotes: 0

Paul Richter
Paul Richter

Reputation: 11072

A simple way you can do this would be to make use of the valueOf method of the Enum class. Something like this:

String name;
...
name = scan.next();
UserClass userClass = UserClass.valueOf(name);

In this case, UserClass is the name of your enum, which might look something like this:

public enum UserClass{
    KNIGHT,
    MAGE,
    DRUID,
    NINJA
    // other classes as needed
}

Note that the scanner line assumes that the user typed in the name correctly. If they did not, you get an IllegalArgumentException, which you can catch with a try...catch block.

Side note: I'm sure you've seen the messages already, but remember that the java standard is for variable names to begin with a lowercase letter. Otherwise it is hard to determine whether the entity in question is a class (which start with uppercase) or a poorly named variable.

Upvotes: 1

Tim B
Tim B

Reputation: 41188

If your enum is called CharacterClass then you have CharacterClass.values() to get a list of the members.

You can then use a for loop to iterate over them printing them out:

for (CharacterClass cc: CharacterClass.values()) {
    System.out.println("\t"+cc);
}

In the long run though you will probably find that enums are too limited for your use. You will probably be better off creating a CharacterClass object (potentially subclassing that in different files for each class) and then having a list of available CharacterClasses

Otherwise unless each class is very simple your enum file will end up being massive and very messy/hard to navigate.

Upvotes: 1

Related Questions