944,012 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3435
  • Java RSS
Mar 29th, 2006
0

missing return statement

Expand Post »
I don't know why I'm getting this return statement 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 }
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gagirl14 is offline Offline
2 posts
since Mar 2006
Mar 29th, 2006
0

Re: missing return statement

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.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Mar 29th, 2006
0

Re: missing return statement

thanks for the help. I didn't notice that extra else statement. It's working now :cheesy:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gagirl14 is offline Offline
2 posts
since Mar 2006
Mar 29th, 2006
0

Re: missing return statement

nice
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Apr 1st, 2006
0

Re: missing return statement

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");

}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
malaya_champ is offline Offline
2 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Txt Coordinates into chart...
Next Thread in Java Forum Timeline: clarify my doubt





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC