2

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

JeyesElite
  • 73
  • 2
  • 6

3 Answers3

1

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.

Tim B
  • 40,716
  • 16
  • 83
  • 128
1

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.

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
  • 1
    Having to enter the full class name and enter it exactly isn't a great user experience, this is why I didn't suggest this method. – Tim B Jan 14 '14 at 14:23
  • The java standard is also for constants names to be all uppercase, and enums are constant. – WilQu Jan 14 '14 at 14:26
  • @TimB That is a very valid point. Ultimately, I didn't want to touch on that since the OP said they're presently learning Java, and I feel they will eventually learn such things down the line once the language-learning hurdle has been passed. Your answer however provides a good alternate perspective. – Paul Richter Jan 14 '14 at 14:26
  • @WilQu Agreed. To the OP, [this answer](http://stackoverflow.com/a/3069863/877472) discusses what WilQu just mentioned. – Paul Richter Jan 14 '14 at 14:28
0

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.

Val
  • 1
  • 8
  • 40
  • 64