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;



}

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.