-3

I have created this function for my game that should read a string, something like "3 Spade" and return its worth (this example it is worth 3 points).

The problem is that I get this error: no return statement found

Although when I write the same exact C++ code it runs without any error (I am coming from C++ background)

The Java code:

public static int getCardValue(String card)
{
    for(int s=0; s != cardSuits.length; s++) //cardSuits is an array of strings 
        for(int r=0; r != cardRanks.length; r++)//cardRanks is an array of strings 
        {
            String GeneratedCard = cardRanks[r] + " " + cardSuits[s];
            if((GeneratedCard).equals(card))
                {
                    if(r >= 0 && r <= 8) //number in array starts from 0(2) to 8(10)
                    {
                        return r;
                    }
                    else if( r >= 8 && r <= 12) //from 8(10) to 11(KING)
                    {
                        return 10;
                    }
                }
            else
             return -1;
        }
}

The same exact C++ code (tested and verified on this specific case for this post)

int Game::func(std::string card)
{
    for (int s = 0; s != 3; s++) //cardSuits is an array of strings 
        for (int r = 0; r != 13; r++)//cardRanks is an array of strings 
        {
            std::string GeneratedCard = cardRanks[r] + " " + cardSuits[s];
            if (GeneratedCard==card)
            {
                if (r >= 0 && r <= 8) //number in array starts from 0(2) to 8(10)
                {
                    return r;
                }
                else if (r >= 8 && r <= 12) //from 8(10) to 11(KING)
                {
                    return 10;
                }
            }
            else
                return -1;
        }
}

I am interested in 2 things:

  1. to know the working version of the java code
  2. why is there this difference between the c++ and the java code
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Eyad
  • 25
  • 6
  • 7
    Because Java is not C++. And C++ is not Java. – Sam Varshavchik Dec 18 '16 at 14:46
  • 1
    if cardSuits.length = 0, then the compiler will not find a return satement. – Owen Delahoy Dec 18 '16 at 14:46
  • As it stands now your question is too broad and/or unclear. –  Dec 18 '16 at 14:46
  • 1
    What do you return if `cardSuits` is empty? What do you return if `cardRanks` is empty? What do you return if `(GeneratedCard).equals(card)` is always *true*, but the inner `if` statements always *false*? – Tom Dec 18 '16 at 14:47
  • @RawN what should i do , to make it more specific? – Eyad Dec 18 '16 at 15:03
  • This question basically comes down to why a certain standard says "A" while another says "B". The c++ standard clearly states *"**Flowing off the end of a constructor, a destructor, or a function with a cv void return type is equivalent to a return with no operand. Otherwise, flowing off the end of a function other than main results in undefined behavior.**"* at [6.6.3/2] – StoryTeller - Unslander Monica Dec 18 '16 at 15:05
  • @Eyad Read the [help section](http://stackoverflow.com/help) to improve future inquires. –  Dec 18 '16 at 15:07
  • oh well, it looks like its just an implementation difference between the 2 languages. as trying those extreme cases that java worries about --- in c++ it just returns 0 instead of complaining and not working at all... thanks for every one answered me... it really helped – Eyad Dec 18 '16 at 15:08
  • 2
    No in c++ it doesn't *"just returns 0 instead of complaining"*. The behavior is undefined. You shouldn't rely on anything specific happening. – StoryTeller - Unslander Monica Dec 18 '16 at 15:08
  • And in Java, this is not an implementation detail. The JLS requires that any way that a non-void method can return must be via an explicit `return` statement which supplies an explicit return value. – Stephen C Dec 18 '16 at 15:20

2 Answers2

2

1- JAVA getCardValue method is a return type function So you should handle the return value in each case even it was a FOR LOOP function as it depends on a condition

public static int getCardValue(String card) {
    for (int s = 0; s != cardSuits.length; s++){
        for (int r = 0; r != cardRanks.length; r++) {
            String GeneratedCard = cardRanks[r] + " " + cardSuits[s];
            if ((GeneratedCard).equals(card)) {
                if (r >= 0 && r <= 8)
                    return r;// handling if condition
                else if (r >= 8 && r <= 12)
                    return 10;// returning value else if condition
            }else{
                return 0;// returning value else condition
            }
        }
    }
    return 0;// returning value FOR loop condition
}

2- I think you should put these return values in C++ function too as it is a return type function

2

Basically, Java is going through your if, else-if, and else statements and making sure that the method always returns a value.
So your code looks like this

if A
  if B
    return
  else if C
     return
else
  return

Therefore, Java can see that if you have a state which is A and not B and not C, there is a logical path that does not return a value.

You could resolve this by putting a default return at the end of the method. Or you could throw an Exception if you think it is an unreachable state because there are only so many cards.

public static int getCardValue(String card)
{
  for (int s = 0; s != cardSuits.length; s++)
    // cardSuits is an array of strings
    for (int r = 0; r != cardRanks.length; r++)// cardRanks is an array of
                                               // strings
    {
      String GeneratedCard = cardRanks[r] + " " + cardSuits[s];
      if ((GeneratedCard).equals(card))
      {
        if (r >= 0 && r <= 8) // number in array starts from 0(2) to 8(10)
        {
          return r;
        }
        else if (r >= 8 && r <= 12) // from 8(10) to 11(KING)
        {
          return 10;
        }
      }
      else
        return -1;
    }
  return -1;
}
ProgrammersBlock
  • 5,974
  • 4
  • 17
  • 21
  • While Java does branch analysis C++ does not, when a C++ function does not return with a return statement. It's return value is undefined, unless it is the main function in which case it returns 0. http://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no – Owen Delahoy Dec 18 '16 at 15:08