MZiskaO MZiskaO - 2 months ago 13
Java Question

Using Switch Statements for Card Program (Java)

I have my program figured out so far, it's just that I'm not understanding these instructions I was given (or at least understanding how to do them).

In my program when I type 10 it prints out "10 of" but when I try to type

10S
for 10 of Spades it only prints out "Spades."

Hopefully someone on here can give me either a solution or a point in the right direction on how to solve my problem:


Use a SWITCH statement to assign the result variable an initial value - the value of the card

Use a second SWITCH statement to concatenate to the result variable the card's suit"


here is the code:

import java.util.*;


public class CardConverter {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

//will hold string that user will input
String card, face, suit, result;

//getting input from user and telling them correct format
System.out.println("Please enter either the number or face value intial of a card followed by the initial of it's suit (ie. QH for Queen of Hearts)");
card = keyboard.nextLine();
//gets first value
face = card.substring(0);
//sets substring for 10 only
//substring for after all single digit/letter card faces
suit = card.substring(1);


//to print face and word of
switch (face)
{
case "10":
System.out.println("10 of ");
break;
case "2":
System.out.println("2 of ");
break;
case "3":
System.out.println("3 of ");
break;
case "4":
System.out.println("4 of ");
break;
case "5":
System.out.println("5 of ");
break;
case "6":
System.out.println("6 of ");
break;
case "7":
System.out.println("7 of ");
break;
case "8":
System.out.println("8 of ");
break;
case "9":
System.out.println("9 of ");
break;
case "J":
System.out.println("Jack of ");
break;
case "Q":
System.out.println("Queen of ");
break;
case "K":
System.out.println("King of ");
break;
case "A":
System.out.println("Ace of ");
break;
}
//to print out card suit
switch (suit)
{
case "H":
System.out.println("Hearts");
break;
case "C":
System.out.println("Clubs");
break;
case "S":
System.out.println("Spades");
break;
case "D":
System.out.println("Diamonds");
break;
}








}


}

Answer

Your problem starts at card.substring(0);, which equals card because the substring from the start of the String. Maybe you wanted card.charAt(0);? But that is also wrong because "10S" will have three characters, two for the face value.

You'll need to handle a three-character input specially or be smarter about the substring-ing.

You know the suit will always be the last character, so use the length of the string to charAt for that.

int suitIndex = s.length() - 1;
String suit = ""+s.charAt(suitIndex);
String face = s.substring(0,suitIndex);

You can also simplify the cases

case "J":
    System.out.println("Jack of ");
    break;
case "Q":
    System.out.println("Queen of ");
    break;
case "K":
    System.out.println("King of ");
    break;
case "A":
    System.out.println("Ace of ");
    break;  
default:
    System.out.println(face + " of "); // handle all the numbers
    break;