0

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.

Alex Lama
  • 9
  • 3

5 Answers5

0

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();
}
Mena
  • 47,782
  • 11
  • 87
  • 106
  • I can't even vote yet! This looks helpful I will try it out now. Thank you – Alex Lama Oct 09 '15 at 10:04
  • Amazing thank you!! this worked perfectly. Except for one thing you wrote `values()` in line 7 but it needed to be `Rank.values()` – Alex Lama Oct 09 '15 at 10:09
  • This would work but is inefficient to loop every time you lookup. Check my answer for a hashtable based solution that I believe is a very elegant and fast solution – triadiktyo Oct 09 '15 at 10:11
  • OK point taken thank you... for the purpose of a class project it should suffice though :) – Alex Lama Oct 09 '15 at 10:18
  • @triadiktyo Not discussing the logic of your solution, but you do know that `HashTable` is obsoleted in favor of `HashMap` for thread-unsafe, and `ConcurrentHashMap` for thread-safe mapping, right? – Mena Oct 09 '15 at 10:45
  • @AlexLama if you're looking to retrieve a `Rank` for both types of input, you can combine `Rank.valueOf(String s)` with my solution, and retrieve the first non-null result. **Edit** you might want to tweak the `NoSuchElementException` throwing in my solution and return `null` instead in that case. – Mena Oct 09 '15 at 10:46
  • Thanks Mena, in fact my program will only ever have `"2"` `"3"` etc. as inputs but I couldn't list these in an `enum` because they have to start with letters so your solution is perfect. – Alex Lama Oct 09 '15 at 10:51
0

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

jacr
  • 1
  • 2
0

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

triadiktyo
  • 479
  • 2
  • 9
0

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;
        }
}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
-1

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());
        }
    }
}
Basit
  • 8,426
  • 46
  • 116
  • 196