How to do this Card class in java?

126 views Asked by At

so basically for my computer science class we have to create a Card class which takes user input for the card notation (for example "4S") and has a getDescription method which returns the description of the card ("four of spades"). the problem is, i keep getting an "unreachable statement" error in regards to the if statement. I probably did the whole thing wrong but here is what i did:

import java.util.Scanner;

public class Card 
{ 

private String face; 
private String suit; 

public Card() 
{ 
Scanner card = new Scanner(System.in); 
System.out.print("Enter the card notation: "); 
String input = card.next(); 

face = input.substring(0,1); 
suit = input.substring(1); 

} 


public String getDescription() 
{ 

return "Your card was the "; 

if (face.equals("A")) return "ace"; 

return getDescription(); 

} 

}

1

There are 1 answers

0
KagelVlusha On

Any code placed after a return statement is unreachable, because the return statement tells a method to exit and go back to wherever it was called from with the information provided. Thus they should go at the end of methods, once you've fully prepared the information you want to send back. Something like

public String getDescription() {
    String s = "Your card was the ";
    if (face.equals("A")) {
        s += "ace";
    };
    return s;
}