Matt Klaver
Matt Klaver

Reputation: 165

How do you convert from a string to an int in Java?

So I have to make a card project that takes a string such as "Six of Hearts" and converts that into an integer array based on the value of the number (six) and the suit (hearts). I'm hitting a wall as to how to get java to take the string "six" and output 6... Any hints?

Edit: Clubs = 0; Spades = 3; Hearts = 2; Diamonds = 1;

Upvotes: 0

Views: 325

Answers (7)

midor
midor

Reputation: 5557

This is typically something you would use an enum for:

enum Suit{
 CLUBS   ("clubs", 0),
 DIAMONDS("diamonds", 1),
 HEARTS  ("hearts", 2),
 SPADES  ("spades", 3);

 private final String name;
 private final int value;
 private static final HashMap<String, Suit> suitByName;
 static {
    suitByName = new HashMap<String, Suit>();
    for (Suit s: Suit.values()){
        suitByName.put(s.name, s);
    }
 }

 Suit(String name, int value){
     this.name = name;
     this.value = value;
 }
 public int getValue(){
    return this.value;
 }

 public static Suit fromString(String card){
    return suitByName.get(card.toLowerCase());
 }
}

Code for card values omitted. Follows the same approach.

Upvotes: 2

Secondo
Secondo

Reputation: 451

Enum is very useful in this kind of scenario .. See Documentation.

First Create an Enum of CardValue eg. ACE TWO..

enum CardValue {

        ACE(1),TWO(2),THREE(3),FOUR(4),
        FIVE(5),SIX(6),SEVEN(7),EIGHT(8),
        NINE(10),TEN(10),JACK(10),QUEEN(10),KING(10);

        private int cardValue;

        private CardValue (int cardValue){
            this.cardValue = cardValue;
        }

        public String toString(){
            return String.valueOf(cardValue);
        }

    }

Second Create an Enum of CardSuit eg. HEARTS CLUBS..

    enum CardSuit {
        DIAMONDS(1),HEARTS(2),SPADES(3),CLUBS(4);
        private int cardSuit;
        private CardSuit(int cardSuit){
            this.cardSuit = cardSuit;
        }

        public String toString(){
            return String.valueOf(cardSuit);
        }

    }

Then try this one.

    public static void main(String[] args){
        String inputedCard = "Six of Hearts";

        //Converts inputed card to uppercase then split it.
        //so the expected value is aInputedCard[0] = SIX & aInputedCard[1] = HEARTS
        String [] aInputedCard = inputedCard.toUpperCase().split(" OF ");

        //So you just need to Verify if the input is in correct format to avoid exceptions

        System.out.println("["+CardValue.valueOf(aInputedCard[0])+"]["+CardSuit.valueOf(aInputedCard[1])+"]");

    }

Upvotes: 0

user3486062
user3486062

Reputation: 1

I think you can use Integer.parseInt(String).

Upvotes: -2

CBredlow
CBredlow

Reputation: 2840

There are multiple ways you can do this, but in my opinion the simplest way would be to have a map of where the string key is the word for the number, i.e. "six", and the value returned is the integer value.

So your code would look like this

HashMap<String, Integer> numMap = new HashMap<String, Integer>();
numMap.put("two", 2);//repeat for all numbers

then whenever you need the digit, you do numMap.get("two")

Upvotes: 1

brso05
brso05

Reputation: 13232

You could use a Map. You could also use a case or if else statements to accomplish this.

    HashMap<String, Integer> numbers = new HashMap<String, Integer>();
    numbers.put("ace", 1);
    numbers.put("two", 2);
    numbers.put("three", 3);
    numbers.put("four", 4);
    numbers.put("five", 5);
    numbers.put("six", 6);
    numbers.put("seven", 7);
    numbers.put("eight", 8);
    numbers.put("nine", 9);
    //etc...
    HashMap<String, Integer> suits = new HashMap<String, Integer>();
    suits.put("clubs", 0);
    suits.put("spades", 3);
    suits.put("hearts", 2);
    suits.put("diamonds", 1);

    numbers.get("zero");//use this to access the number
    suits.get("spades");//use this to access the suit

Case sensitivity will matter here so make sure the key matches whatever you pass to access the value...

Upvotes: 3

La-comadreja
La-comadreja

Reputation: 5755

Another approach, based on finding the index of an element within an array:

String str = "Six of Hearts"; //input string
String[] numbers = new String[]{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", "ace"};
String[] suits = new String[]{"clubs", "diamonds", "hearts", "spades"};
String[] name = str.split(" ");
if (name.length != 3) throw new Exception("Improperly formatted card name");
int value = Arrays.asList(numbers).indexOf(name[0]);
int suit = Arrays.asList(suits).indexOf(name[2]);

Upvotes: 0

Orel Eraki
Orel Eraki

Reputation: 12196

You can mix it up a little bit.

  1. Split the input string into array of string. B. In the resulted
  2. Array at index cell 0 it will contain the Number. C. In the resulted
  3. Array at index cell 2 it will contain the Shape.

Now all that you need is to convert Number into actual number. This can be achieved by Switch or Map.

The same goes for converting Shape into actual number that represent the shape.

Upvotes: 0

Related Questions