Noobprogrammer
Noobprogrammer

Reputation: 19

array method throwing class expected error

Not sure why this error is coming up:

class expected deck[].shuffleDeck();

error: ';' expected deck[].shuffleDeck();

The code:

public class Main {

    int index;
    Card [] deck;

    public void shuffleDeck(){
        for (int i = 0; i<deck.length; i++){
            index = (int)(Math.random()*deck.length);
            deck[index] = deck[i];
        }
    }

    public String dealCard(){
        String dealer = deck[0] + " ";
        return dealer;
        }



public static void main(String[] args) {
    Card card0 = new Card();
    Card card1 = new Card();
    Card card2 = new Card();
    Card card3 = new Card();
    Card card4 = new Card();
    Card card5 = new Card();
    Card card6 = new Card();
    Card card7 = new Card();
    Card card8 = new Card();
    Card card9 = new Card();
    Card card10 = new Card();
    Card card11 = new Card();
    Card card12 = new Card();
    Card card13 = new Card();
    Card card14 = new Card();
    Card card15 = new Card();
    Card card16 = new Card();
    Card card17 = new Card();
    Card card18 = new Card();
    Card card19 = new Card();
    Card card20 = new Card();
    Card card21 = new Card();
    Card card22 = new Card();
    Card card23 = new Card();
    Card card24 = new Card();
    Card card25 = new Card();
    Card card26 = new Card();
    Card card27 = new Card();
    Card card28 = new Card();
    Card card29 = new Card();
    Card card30 = new Card();
    Card card31 = new Card();
    Card card32 = new Card();
    Card card33 = new Card();
    Card card34 = new Card();
    Card card35 = new Card();
    Card card36 = new Card();
    Card card37 = new Card();
    Card card38 = new Card();
    Card card39 = new Card();
    Card card40 = new Card();
    Card card41 = new Card();
    Card card42 = new Card();
    Card card43 = new Card();
    Card card44 = new Card();
    Card card45 = new Card();
    Card card46 = new Card();
    Card card47 = new Card();
    Card card48 = new Card();
    Card card49 = new Card();
    Card card50 = new Card();
    Card card51 = new Card();


    card0.setCard(1, 1);
    card1.setCard(2, 1);
    card2.setCard(3, 1);
    card3.setCard(4, 1);
    card4.setCard(5, 1);
    card5.setCard(6, 1);
    card6.setCard(7, 1);
    card7.setCard(8, 1);
    card8.setCard(9, 1);
    card9.setCard(10, 1);
    card10.setCard(11, 1);
    card11.setCard(12, 1);
    card12.setCard(13, 1);
    card13.setCard(1, 2);
    card14.setCard(2, 2);
    card15.setCard(3, 2);
    card16.setCard(4, 2);
    card17.setCard(5, 2);
    card18.setCard(6, 2);
    card19.setCard(7, 2);
    card20.setCard(8, 2);
    card21.setCard(9, 2);
    card22.setCard(10, 2);
    card23.setCard(11, 2);
    card24.setCard(12, 2);
    card25.setCard(13, 2);
    card26.setCard(1, 3);
    card27.setCard(2, 3);
    card28.setCard(3, 3);
    card29.setCard(4, 3);
    card30.setCard(5, 3);
    card31.setCard(6, 3);
    card32.setCard(7, 3);
    card33.setCard(8, 3);
    card34.setCard(9, 3);
    card35.setCard(10, 3);
    card36.setCard(11, 3);
    card37.setCard(12, 3);
    card38.setCard(13, 3);
    card39.setCard(1, 4);
    card40.setCard(2, 4);
    card41.setCard(3, 4);
    card42.setCard(4, 4);
    card43.setCard(5, 4);
    card44.setCard(6, 4);
    card45.setCard(7, 4);
    card46.setCard(8, 4);
    card47.setCard(9, 4);
    card48.setCard(10, 4);
    card49.setCard(11, 4);
    card50.setCard(12, 4);
    card51.setCard(13, 4);



    Card [] deck = {card0, card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13,
    card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26, card27, card28,
        card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39, card40, card41, card42, card43,
        card44, card45, card46, card47, card48, card49, card50, card51,
    };

    deck[].shuffleDeck();

}

}

Just for clarification, this method must be created in the Main class, and not in the card class. If I simply put this in the body of the main method it works, but that's not what is asked of us. Also, I'm pretty sure if I try to call the dealCard method afterwards the same error will be thrown.

I'm not sure what I've done incorrectly, have I invoked the method incorrectly? or declared the method in the wrong place? It has to be in the Main class, so I think I've invoked it incorrectly.

I looked at a few other problems with array methods with the same error, but they were not helpful, and unfortunately my textbook chapter on arrays does not have one single example of invoking array methods.

Thank you in advance, and I apologize if this is a rudimentary problem that I should be able to solve on my own, but I'm just not seeing what I have to do.

Upvotes: 0

Views: 250

Answers (2)

AztecCodes
AztecCodes

Reputation: 1

This doesn't work like that.

The method shuffleDeck() is not from the class Card[]. Also you shouldn't define "Card[] deck" twice.

You have to give you Card[] Object "deck" as a parameter for the method shuffleDeck().

See here:

    public void shuffleDeck(Card[] givenCards){

    for (int i = 0; i<givenCards.length; i++){
        index = (int)(Math.random()*givenCards.length);
        givenCards[index] = givenCards[i];

    }
}

To execute the method with Card[] card in it you have to do this:

shuffleDeck(card);

You have to create a new class and create an instance of it if you want to execute the method without setting everything static. The things you want to execute when an instance of your class is executed you have to put into the constructor.

For example create the class App.

Class App:

public class App {

 public static void main(String[] args) {
     YourClass newClass = new YourClass();
     
   }
}

Upvotes: 1

Murat Tz
Murat Tz

Reputation: 1

For running shuffleDeck() you must create an instance of Main object since the function belongs to the Main class. And also you should initialize deck variable of the Main class.

Upvotes: 0

Related Questions