I don't know why I'm getting this return statement :sad: Please help. Error is in green

public class Card {


private int rank;
private int suit;


public Card()
{
suit = (int)Math.floor(Math.random() * (4-1) + 0.5) +4;
rank = (int)Math.floor(Math.random() * (14-2) + 0.5) +14;
}
public Card(int r, int s)
{
if (r>=2 && r<=14)
rank = r;
else rank = 14;


if (s>=1 && s<=4)
suit = s;
else suit = 4;
}
public int getSuit() //returns suit
{
return suit;
}
public int getRank() //return rank
{
return rank;
}


public boolean isHigher(Card c)
{
if (c.getRank() >rank){ //if Card is higher in rank return true
return true;
}
else
if (c.getRank() < rank){ //if Card is lower in rank return false
return false;
}
else
if (c.getRank() ==rank){   //if rank is equal then evaluate suit


if (c.getSuit() >suit){
return true;
}
else
if (c.getSuit() <suit){
return false;
}
}


I get error here  }
public String toString()
{
String strRank = "";
String strSuit = "";


if (rank ==14){
strRank = "A";
}


else
if (rank ==13){
strRank = "K";
}
else
if (rank ==12){
strRank = "Q";
}
else
if (rank ==11){
strRank = "J";
}
else
if (rank ==10){
strRank = "10";
}
else
if (rank ==9){
strRank = "9";
}
else
if (rank ==8){
strRank = "8";
}
else
if (rank ==7){
strRank = "7";
}
else
if (rank ==6){
strRank = "6";
}
else
if (rank ==5){
strRank = "5";
}
else
if (rank ==4){
strRank = "4";
}
else
if (rank ==3){
strRank = "3";
}
else
if (rank ==2){
strRank = "2";
}
else
if (rank ==1){
strRank = "1";
}


if (suit==4){
strSuit= "Spades";
}
else
if (suit==3){
strSuit= "Hearts";
}
else
if (suit==2){
strSuit= "Diamonds";
}
else
if (suit==1){
strSuit= "Club";
}
else


return strRank + "of" + strSuit;

and i get the same missing return statement error here

Recommended Answers

All 4 Replies

first, you have a lot of if/else statemets that go on for more then 1 line so you need brackets. I ususual bracket no matter what just to be on the safe side. Another thing with the return statements, if you return something inside if statements it could be possible that it never tests it, so you need one at the end of the method definition. The last else statement doesn't seem like it is doing anything so you could remove it.
Well i think that's all the problems, post if you have any more questions or problems with it.

thanks for the help. I didn't notice that extra else statement. It's working now :cheesy:

public class Card 
{

private int rank;
private int suit;

public Card() 
{
suit = (int)Math.floor(Math.random() * (4-1) + 0.5) +4; 
rank = (int)Math.floor(Math.random() * (14-2) + 0.5) +14; 
}
public Card(int r, int s) 
{
if (r>=2 && r<=14)
rank = r;
else rank = 14; 

if (s>=1 && s<=4)
suit = s;
else suit = 4; 
}
public int getSuit() //returns suit
{
return suit;
}
public int getRank()             //return rank
{
return rank;
}

public boolean isHigher(Card c) 
{
if (c.getRank() >rank)
{                        //if Card is higher in rank return true
return true;
} 
else
if (c.getRank() < rank)
{                      //if Card is lower in rank return false
return false;
} 
else
if (c.getRank() ==rank)
{                       //if rank is equal then evaluate suit

if (c.getSuit() >suit)
{
return true;
}
else
if (c.getSuit() <suit)
{
return false;
}
} 
return true;

                         //I get error here 
}
public String toString() 
{
String strRank = "";
String strSuit = "";

if (rank ==14){
strRank = "A";
}

else
if (rank ==13){
strRank = "K";
}
else
if (rank ==12){
strRank = "Q";
}
else
if (rank ==11){
strRank = "J";
}
else
if (rank ==10){
strRank = "10";
}
else
if (rank ==9){
strRank = "9";
}
else
if (rank ==8){
strRank = "8";
}
else
if (rank ==7){
strRank = "7";
}
else
if (rank ==6){
strRank = "6";
}
else
if (rank ==5){
strRank = "5";
}
else
if (rank ==4){
strRank = "4";
}
else
if (rank ==3){
strRank = "3";
}
else
if (rank ==2){
strRank = "2";
}
else
if (rank ==1){
strRank = "1";
}

if (suit==4){
strSuit= "Spades";
}
else
if (suit==3){
strSuit= "Hearts";
}
else
if (suit==2)
{
strSuit= "Diamonds";
}
else
if (suit==1){
strSuit= "Club";
}
else

return strRank + "of" + strSuit; 
return strSuit;
                        // i get the same missing return statement error here }
}
public static void main(String mpo[])
{
    System.out.println("rroop");

}
}
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.