Skip to main content
gamedev.net
🔒 Locked

[java] Is it NetBeans or Java?

Started by Alpha_ProgDes 3 replies 750+ views
Original Post
Alpha_ProgDes
Alpha_ProgDes
I don''t understand why this code doesn''t work the way I think it''s supposed to.
public Card newCard (String c, String s) {
        // pick a card outta the deck.

        // match it with the selection given

        // if not there, put it in the empty

        // deck.

        Card foundNewCard = null;
        foundNewCard = NewDeck.remove();
        for (int i = 0; i < NewDeck.size(); ++i) {
            if (foundNewCard.getFace() == c && foundNewCard.getSuit() == s) {
                return foundNewCard;
            }
            else {
                EmptyDeck.placeCard(foundNewCard);
                foundNewCard = NewDeck.remove();
            }
        }
        
    }
But! this code works just fine.
public Card newCard (String c, String s) {
        // pick a card outta the deck.

        // match it with the selection given

        // if not there, put it in the empty

        // deck.

        Card foundNewCard = null;
        foundNewCard = NewDeck.remove();
        for (int i = 0; i < NewDeck.size(); ++i) {
            if (foundNewCard.getFace() == c && foundNewCard.getSuit() == s) {
                break;
            }
            else {
                EmptyDeck.placeCard(foundNewCard);
                foundNewCard = NewDeck.remove();
            }
        }
        return foundNewCard;
    }
I had a problem similar to this before but with a switch statement. Could someone please tell me if I''m doing a programming no-no, if I am, tell why is it a no-no and what is the "official" rule??
Beginner in Game Development?  Read here. And read here.  
SantaClaws
SantaClaws
What actually happens, or doesn''t work?
Alpha_ProgDes
Alpha_ProgDes
the first piece of code gives me an error saying: missing return statement and points to the last brace in the function.

the other code compiles fine.

but they should do the same thing though. shouldn''t they?
Beginner in Game Development?  Read here. And read here.  
Jansic
Jansic
quote:
the first piece of code gives me an error saying: missing return statement and points to the last brace in the function.


...and it''s not wrong - there is an execution path through the first function that potentially returns no result. Hence the missing return statement error message.

Jans.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.