m bunn
m bunn

Reputation: 15

how do I properly split this string after the first position (0) so that the one or more characters after 0 are read?

Basically I have this code, that with the input of 2 OR 3 letters/numbers combos like (AH, 10D, JS, and 7C) read back as value's of suits (Ace of Hearts, 10 of Diamonds, Jack of Spades, 7 of Clubs.)

Why, if I input "10D", does the code print nothing back? Why is it not register 10? How can I fix this? ALSO: Please help me figure out how to shorten the case 2 - case 10 range into something more eloquent? I cannot use if-then-else statements.

System.out.print("Please enter a letter/integer of a playing card (A, J, Q, K, or 2 - 10),\nfollowed by card type (D, H, S, C):");
    Scanner kbd = new Scanner(System.in);
    String userInput = kbd.next().toUpperCase();
    String valueofCard = userInput.substring(0, userInput.length() / 2);  // gives first half of string
    String suitofCard = userInput.substring(userInput.length() / 2); //give last half of string with + 1 if odd
    StringBuilder result = new StringBuilder();

    switch (valueofCard) {
    case "A":
        result.append("Ace of ");
        break;
    case "J":
        result.append("Jack of ");
        break;  
    case "Q":
        result.append("Queen of ");
        break;  
    case "K":
        result.append("King of ");
        break;
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
    case "10":          
        result.append(valueofCard + " of ");
    break;  
        }

    switch (suitofCard) {
    case "D":
        result.append("Diamonds");
        break;  
    case "H":
        result.append("Hearts");
        break;      
    case "S":
        result.append("Spades");
        break;      
    case "C":
        result.append("Clubs");
        break;
        }

    System.out.println(result.toString());

    kbd.close();

Upvotes: 0

Views: 62

Answers (3)

Andreas
Andreas

Reputation: 159155

If you use a regular expression, it will also reject bad input for you.

Regex: ([AJQK2-9]|10)([DHSC])

Sample code:

String card = "QS"; // Queen of Spades

Pattern cardPattern = Pattern.compile("([AJQK2-9]|10)([DHSC])");

Matcher m = cardPattern.matcher(card);
if (! m.matches()) {
    System.out.println("Not a valid card: " + card);
} else {
    String rank = m.group(1);
    String suit = m.group(2);
    // code here
}

Upvotes: 1

1Poseidon3
1Poseidon3

Reputation: 135

I believe the problem is that, as an integer, 3 / 2 = 1 so when you input 10D, the substring reads the position counter as 1 instead of 2 which makes the string 1 instead of 10, which isn't handled by your cases.

I would recommend using either

if(userInput.length()>2)
{
    String valueofCard = userInput.substring(0,2);
    String suitofCard = userInput.substring(2);
}
else
{
    String valueofCard = userInput.substring(0,1);
    String suitofCard = userInput.substring(1);
}

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44854

It is simpler to code to the exceptional case(s)

Like

String valueofCard = userInput.substring(0, 1);
// unless starts with "10"
if (userInput.startWith ("10")) {
  valueofCard = userInput.substring(0, 2);     
}

Upvotes: 0

Related Questions