0

I have this piece of code down here - the PlayingDeck class. It's not done, as you see, but what happens is that on line 17, Right under the for loop, I get an error message saying the PlayingCard declaration and initialization isn't a statement. Why so? When I add the curly brackets to the for loop with just that same statement, the error message disappears. Could someone please explain why? All I need are two brackets, one at the start and one at the end of the for loop...?

Thank you

Code that does not compile --------

public class PlayingDeck {
    static int numberOfCards;

    static {
        numberOfCards = 52;
    }

    PlayingCard[] playingDeckArray;

    PlayingDeck() {
        playingDeckArray = new PlayingCard[numberOfCards];

        for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++)
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
    }

    public static void main(String[] args) {

    }
}

Code that Compiles -----

public class PlayingDeck {
    static int numberOfCards;

    static {
        numberOfCards = 52;
    }

    PlayingCard[] playingDeckArray;

    PlayingDeck() {
        playingDeckArray = new PlayingCard[numberOfCards];

        for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++){
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
        }
    }

    public static void main(String[] args) {

    }
}

2 Answers 2

1

It is not allowed because the declaration of PlayingCard currentCard has no scope if you don't wrap it with braces.

BTW, if you want your loop to do something, you should probably change the condition to currentCardNumber < 51;

Sign up to request clarification or add additional context in comments.

3 Comments

Is it fair to say that it has a scope, but that scope is useless?
Or even change the condition to currentCardNumber < numberOfCards.
I guess that make sense because when I do this for loop everything compiles just fine: PlayingDeck() { DeckArray = new PlayingCard[numOfSuits][numOfRanks]; for (int currentSuitNumber = 1; currentSuitNumber <= numOfSuits; currentSuitNumber++) { for (int currentRankNumber = 1; currentRankNumber <= numOfRanks; currentRankNumber++) DeckArray[currentSuitNumber - 1][currentRankNumber - 1] = new PlayingCard(currentSuitNumber, currentRankNumber); } } But there is no variable declaration here so I guess curly brackets aren't necessary. Thanks.
1

Your problem is in the for loop

for (int currentCardNumber = 0; currentCardNumber > 51; currentCardNumber++)
            PlayingCard currentCard = playingDeckArray[currentCardNumber];
}

The opening curly brace { is missing.

That's a standard for any language, not only java. Any loop, whether it be for loop, or while loop or do-while loop, or any block like if-else, if-else if- else, switch must have opening and closing curly braces.

Read this for more details: Control Flow Statements in Java

Also read this: Wikipedia - Indent Style

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.