-5

For my program I have a large number of Public static int, it's mainly a readability thing so I can do something like SkillsArray[Archery] = 25 to see at a glance that the "archery skill" has a value/level/rank of 25 instead of trying to figure out what SkillsArray[7] = 25 is supposed to mean.

It's basically naming the unique Id to be used in a relational database structure but if I want to print those names I nave to build a function that takes a int argument that reads a huge if/else tree to spit out a string with the name "Archery" or "cooking" or whatever.

I'd like to replace all those functions with something simple like

String printStaticName() {
    return this.getClass().getName();
}

which can read the primitive attached to it and get its name but Eclipse tells me I can't do those kind of operations on a primitive type. Ideas?

A example of how I currently use the system i have.

//races
public static final int Chosen = 0;
public static final int Native = 1;
public static final int High_Elf = 2;
public static final int Wood_Elf = 3;
public static final int Gremlin = 4;
public static final int Goblin = 5;
public static final int Hobgoblin = 6;
    ....

String getRaceNameString(int r){
    String temp = "No Race Set";
    if (r == Chosen){temp = "Chosen";};
    if (r == Native){temp = "Native";};
    if (r == High_Elf){temp = "High Elf";};
    if (r == Wood_Elf){temp = "Wood Elf";};
    if (r == Gremlin){temp = "Gremlin";};
    if (r == Goblin){temp = "Goblin";};
    if (r == Hobgoblin){temp = "Hobgoblin";};
    return temp;
}//close getRaceNameString

int getRaceIDFromString(String r){
    int result = 0;
    if (r == "Chosen"){result = Chosen;};
    if (r == "Native"){result = Native;};
    if (r == "High Elf"){result = High_Elf;};
    if (r == "Wood Elf"){result = Wood_Elf;};
    if (r == "Gremlin"){result = Gremlin;};
    if (r == "Goblin"){result = Goblin;};
    if (r == "Hobgoblin"){result = Hobgoblin;};
    return result;
}//close getraceIDFromString()

    class Race extends WoSCharacter {
private int raceID = Chosen;
private double purity = 1;
private String active_headshot = "headshot.jpg";
private String active_fullbody = "TemplateforPortrait.png";
private String Description;
private double minHeight;
private double maxHeight;
private int AttributeMods[] = new int[9];
private double lifespan;
private boolean isModRace = false;

void setRace(int RName, long pure, int gender){
    purity = pure;
    raceID = RName;
    switch (raceID){
    //region case
    case Chosen:
        AttributeMods[STR] = 5;
        AttributeMods[DEX] = 5;
        AttributeMods[CON] = 5;
        AttributeMods[CHA] = 5;
        AttributeMods[PER] = 5;
        AttributeMods[WIL] = 5;
        AttributeMods[INT] = 5;
        AttributeMods[INS] = 5;
        AttributeMods[WIS] = 5;
        active_headshot = "headshot.jpg";
        Description = "The race of man are the sole survivors of the Mage Wars that took destroyed magic and everything but the animal races and humanity. Now they are facing their own extinction as the Black Plague washes over the world, crossing continents and destroying entire family lines. Some of mankind have developed an immunity, being christened 'the chosen' but this this comes at cost as they are further divorced from the just now returning powers of magic.";
        minHeight = 150;
        maxHeight = 170;
        lifespan = 50;
        if (gender == MALE){
            active_headshot = "male_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 162;
            maxHeight = 190;
            lifespan = 49;
        }else if (gender == FEMALE){
            active_headshot = "Fem_headshot.jpg";
            active_fullbody = "Fantasy_Landscape_01.jpg";
            minHeight = 150;
            maxHeight = 170;
            lifespan = 43;
        }//gender headshot switch
        break;
        //endregion case
    case Native:
        AttributeMods[STR] = 5;
        AttributeMods[DEX] = 5;
        AttributeMods[CON] = 5;
        AttributeMods[CHA] = 5;
        AttributeMods[PER] = 5;
        AttributeMods[WIL] = 5;
        AttributeMods[INT] = 5;
        AttributeMods[INS] = 5;
        AttributeMods[WIS] = 5;
        active_headshot = "headshot.jpg";
        Description = "The race of man are the sole survivors of the Mage Wars that took destroyed magic and everything but the animal races and humanity. Now they are facing their own extinction as the Black Plague washes over the world, crossing continents and destroying entire family lines. Some of mankind have developed an immunity, being christened 'the chosen' but this this comes at cost as they are further divorced from the just now returning powers of magic.";
        minHeight = 150;
        maxHeight = 170;
        lifespan = 49;
        if (gender == MALE){
            active_headshot = "male_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 162;
            maxHeight = 190;
            lifespan = 49;
        }else if (gender == FEMALE){
            active_headshot = "Fem_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 150;
            maxHeight = 170;
            lifespan = 43;
        }//gender headshot switch
        break;
      }

    }
NekoLLX
  • 219
  • 6
  • 19
  • 4
    First, write your question using moderate language, this is not a forum but a question and answer site for **professionals**. Second, you can't do what you want in an array (and Eclipse is telling you), instead use a `Map`. – Luiggi Mendoza Jun 16 '13 at 16:58
  • Rather than the giant if else structure why dont you just have an array of strings called names and just put names [archery]="archery" then you can get the name with ease (doesnt solve your full problem but its an improvement – Richard Tingle Jun 16 '13 at 17:01

2 Answers2

2

Use an enum instead (provided your skill set is fixed):

public enum Skill
{
    ARCHERY,
    ETC
}

And in order to build a skill "array", use an EnumMap. You could do, in the enum for instance:

public enum Skill
{
    ARCHERY,
    ETC;

    public static Map<Skill, Integer> newSkillMap()
    {
        final Map<Skill, Integer> ret = new EnumMap(Skill.class);
        for (final Skill skill: values())
            ret.put(skill, 0);
        return ret;
    }
}

Usage:

final Map<Skill, Integer> skillMap = Skill.newSkillMap();
map.get(Skill.ARCHERY); // skill for archery
map.put(Skill.ARCHERY, 30); // change value

You can even improve your enum so as to have default values, etc; enums can do a lot of things in Java. Sample:

public enum Skill
{
    ARCHERY(10, "archery"),
    ETC(0, "etc");

    private final int defaultValue;
    private final String description;

    Skill(final int defaultValue, final String description)
    {
        this.defaultValue = defaultValue;
        this.description = description;
    }

    public static Map<Skill, Integer> newSkillMap()
    {
        final Map<Skill, Integer> ret = new EnumMap(Skill.class);
        for (final Skill skill: values())
            ret.put(skill, skill.defaultValue);
        return ret;
    }

    @Override
    public String toString()
    {
        return description;
    }
}
fge
  • 119,121
  • 33
  • 254
  • 329
  • The only downside here is that you if you want to add more skills you must recompile the application (or this jar). If OP's following a good design, the skills would be loaded separately from a file or another data source. – Luiggi Mendoza Jun 16 '13 at 17:04
  • Yes, that is true. But I suppose the skill set is fixed ;) – fge Jun 16 '13 at 17:06
  • It would be better to suppose nothing or providing a comment explaining what you're supposing. – Luiggi Mendoza Jun 16 '13 at 17:07
0

You can use an enum for your purpose:

public enum Skill {
  SWORD(0, "Sword"),
  ARCHERY(1, "Archery")
  ;

  public final String name;
  public final int index;

  Skill(int index, String name) {
    this.name = name;
    this.index = index;
  }
}

skillsArray[ARCHERY.index] = 25;
System.out.println(ARCHERY.name+" is +"skillsArchery[ARCHERY.index]);

Mind that you can use ARCHERY.ordinal() if you prefer the builtin index assigned to your enum value. That will be safer but less flexible (since it will depend on position imposed in enum declaration).

Jack
  • 131,802
  • 30
  • 241
  • 343
  • i tried using enums before but could never get the results i want. A bit problem i have is i use the int for readability in naming catagories like Race names, class names, skills, then have a class that uses....hold on let me just modify the OP to illistrate – NekoLLX Jun 16 '13 at 18:40