public class Card {

    public Card(String cardValue)
    {
    	input = cardValue;
    }
    public String getDescription()
    {
    if (input.length() == 2)
    
    {
    	if(input.substring(0,1).equals("A"))
    			first = "Ace of ";
    		else if(input.substring(0,1).equals("2"))
    			first = "Two of ";
    		else if(input.substring(0,1).equals("3"))
    			first = "Three of ";
    		else if(input.substring(0,1).equals("4"))
    			first = "Four of ";
    		else if(input.substring(0,1).equals("5"))
    			first = "Five of ";
    		else if(input.substring(0,1).equals("6"))
    			first = "Six of ";
    		else if(input.substring(0,1).equals("7"))
    			first = "Seven of ";
    		else if(input.substring(0,1).equals("8"))
    			first = "Eight of ";
    		else if(input.substring(0,1).equals("9"))
    			first = "Nine of ";
    		else if(input.substring(0,1).equals("J"))
    			first = "Jack of ";
    		else if(input.substring(0,1).equals("Q"))
    			first = "Queen of ";
    		else if(input.substring(0,1).equals("K"))
    			first = "King of ";
    	else first = "Unknown of ";
    }
		if (input.length() == 3)
    	{
    		if(input.substring(0,2).equals("10"))
    			first = "Ten of ";
    		else if(input.substring(2,3).equals("D"))
    			third = "Diamonds";
    		else if(input.substring(2,3).equals("H"))
    			third = "Hearts";
    		else if(input.substring(2,3).equals("S"))
    			third = "Spades";
    		else if(input.substring(2,3).equals("C"))
    			third = "Clubs";
    		else third = "Unknown";
    	}
    		if(input.substring(1,2).equals("D"))
    			second = "Diamonds";
    		else if(input.substring(1,2).equals("H"))
    			second = "Hearts";
    		else if(input.substring(1,2).equals("S"))
    			second = "Spades";
    		else if(input.substring(1,2).equals("C"))
    			second = "Clubs";
    		else second = "Unknown";

    		return first + second;


    }
    	private String first;
    	private String second;
    	private String third;
    	private String input;



}

Dani AI

Generated

Root cause: the rank and suit checks are mixed together and use the wrong substring indices for a 3-character input. As pointed out, the code assigns a third variable for "10X" cards but the method returns first + second, so that assigned suit is never used. Also the if (input.substring(0,2).equals("10")) is in the same else-if chain as the suit tests, so when "10" matches the chain exits and the suit tests never run. correctly called out the variable confusion.

Fix approach (robust and simple): treat the last character as the suit and everything before it as the rank. This avoids index mistakes for both 2- and 3-character inputs, removes the need for a separate third variable, and makes the logic clear. Validate input (null/trim/uppercase) and map rank and suit independently.

Example implementation (minimal, safe):

public String getDescription() {
    if (input == null) return "Invalid card";
    String s = input.trim().toUpperCase();
    if (s.length() < 2) return "Unknown";

    String suitChar = s.substring(s.length() - 1);
    String rankPart = s.substring(0, s.length() - 1);

    String rank;
    switch (rankPart) {
        case "A":  rank = "Ace of "; break;
        case "10": rank = "Ten of "; break;
        case "J":  rank = "Jack of "; break;
        case "Q":  rank = "Queen of "; break;
        case "K":  rank = "King of "; break;
        default:   rank = (rankPart.matches("\\d") ? rankPart + " of " : "Unknown of ");
    }

    String suit;
    switch (suitChar) {
        case "H": suit = "Hearts"; break;
        case "D": suit = "Diamonds"; break;
        case "S": suit = "Spades"; break;
        case "C": suit = "Clubs"; break;
        default:  suit = "Unknown";
    }

    return rank + suit;
}

Notes: remove unused fields (third), always use braces for clarity, add unit tests for inputs like "AH", "10H", "2D", and add optional enums or a Map for cleaner rank/suit lookups in larger code. Debug by printing rankPart and suitChar if unexpected results appear.

Recommended Answers

All 6 Replies

Tester...

import javax.swing.JOptionPane;

public class CardTester
{
	public static void main(String[] args)
	{
		String input = JOptionPane.showInputDialog("Enter the card notation");
		Card value = new Card(input);
		System.out.println(value.getDescription());
		System.exit(0);
	}
}

You've got a couple of errors.
1. When the string is of length 3 you assign a variable "third" a value but this value is never returned.
2. Your if-else statements are wrong @ lines 40-42 (maybe somewhere else too)

if(input.substring(0,2).equals("10"))
  first = "Ten of ";
else if(input.substring(2,3).equals("D"))
  third = "Diamonds";

Consider what the above code will do to the input "10D"

You should really consider using { } for your if-statements, it's good practice even though it's not needed

I already had a "third" variable though. Im confused.

Does anyone know? simplicity.

What first, second, third, variable for? You are NOT returning "third" but first+second even though you assigned value to "third" in your checking?

You need to modify some in your codes...

1)Change all "third" variable name to "second" in line 43 to 50.
2)Move your code line 52 to 60 to be below line 36 which is still inside the if(input.length()==2) scope.
3)Remove the variable "third" in line 68.

Thank-you Taywin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.