简体   繁体   中英

Assign enum values based on value parameter in JAVA

I have a card class:

public class Card{        
    private final Rank rank;
    public Rank rank() { return rank; }
    public Card(Rank rank) {
        this.rank = rank;
    }    
}

Where Rank is an enum defined as so:

enum Rank {
    TWO ("2"),
    THREE ("3"),
    FOUR ("4"),
    private final String rankNum;
    private Rank(String rankNum) { this.rankNum = rankNum; }
    public String rankNum(){ return rankNum; }
}

So my goal is to input a string "2" as a command line argument and then make a new card object with its rank value as TWO. If the input were "TWO" then it would be possible to do this but I do not know how to do it if the input is "2".

If the input were "TWO" I would do the following:

Card firstCard = new Card(Rank.args[0]);

I hope this question wasn't too badly phrased or obvious, as you can tell I'm still in my early stages, but I've searched this question for hours to no avail.

If your expected input is "2" (as opposed to "TWO" ), you could add a utility static method in your enum.

Something in the lines of:

public static Rank forValue(String value) {
    // null/empty check
    if (value == null || value.isEmpty()) {
        throw new IllegalArgumentException();
    }
    // iterates enum values
    for (Rank r : Rank.values()) {
        // found a match
        if (r.rankNum().equals(value)) {
            return r;
        }
    }
    // iteration over, no match found
    throw new NoSuchElementException();
}

You can't do that. You should check if your command line argument is equals to any Rank value, and then create your new Card

Use Rank.lookupByNumber("2") to get Rank.TWO

import java.util.HashMap;

enum Rank {
    TWO("2"), THREE("3"), FOUR("4");

    private static final HashMap<String, Rank> lookupTable = new HashMap<>();

    static {
        for (Rank rank : Rank.values()) {
            lookupTable.put(rank.rankNum, rank);
        }
    }

    private final String rankNum;

    private Rank(String rankNum) {
        this.rankNum = rankNum;
    }

    public String rankNum() {
        return rankNum;
    }

    public static Rank lookupByNumber(String number) {
        return lookupByNumber(number);
    }
}

In a static block you initialize a lookupTable with the strings you provide as keys. So you can very easily and very quickly grab the Rank enum by the String provided

There is no other way then loop through the values of your Enum , and check if your given value matches with any value in the Enum constants.

String value = //given value Ex: "2"
for (Rank rank : Rank.values()) {
        if (rank.rankNum().equals(value)) {
            return rank;
        }
}

What about this

public enum Rank {

    TWO ("2"),
    THREE ("3"),
    FOUR ("4");

    private String description;

    private Rank(final String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return this.description;
    }

Main:

public static void main( String[] args ) throws Exception {

    if(args.length > 0) {
        String rank = args[0].trim();
        if (rank.equals(Rank.TWO.toString())) {
            System.out.println("entered rank is: " + Rank.TWO.getDescription());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM