Hello! I'm working off of a problem in my "How To Think Like A Computer Scientist" textbook. The exercise in the book is having me work with a deck of cards.

I receive two errors in my current code. The first is that every method after Class Card {...} says "Exporting non-public type through public API". Is this something to worry about?

The second error is in the method buildDeck. I get "non-static variable this cannot be referenced from a static content". I highlighted the line with this error in bold.

Any advice?

package handscore;

/**
 *
 * @author Josh
 */
public class Main {
    class Card {
        int suit, rank;
        
        public Card () {
            this.suit = 0;
            this.rank = 0;
        }
        public Card (int suit, int rank) {
            this.suit = suit;
            this.rank = rank;
        }
    }
    // prints all cards
        public static void printCard (Card c) {
            String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
            String[] ranks = {"empty", "Ace", "2", "3", "4" ,"5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
            System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
    }// compares two cards and determines whether they are equal
        public static boolean sameCard (Card c1, Card c2) {
            return (c1.suit == c2.suit && c1.rank == c2.rank);
        }
        // compares two cards and determines whether or not the first is greater
        // than the second
        public static int compareCard (Card c1, Card c2) {
            if (c1.suit > c2.suit) return 1;
            if (c1.suit < c2.suit) return -1;
            if (c1.rank > c2.rank) return 1;
            if (c1.rank < c2.rank) return -1;
            return 0;
        }
        
        public static Card[] buildDeck () {
            Card[] deck = new Card [52];
            
            int index = 0;
            for (int suit = 0; suit <= 3; suit++) {
                for (int rank = 1; rank <= 13; rank++) {
                   [B] deck[index] = new Card (suit, rank);[/B]
                    index++;
                }
            }
            return deck;
    }

    
    public static void main(String[] args) {
        // TODO code application logic here
    }

}

For some reason my code was altered while I revised.
The second error is in the last method, the line;
deck[index] = new Card (suit, rank);

For some reason your code doesn't have line numbers. Usually when you post with code tags, it numbers the lines. Anyway, you have an extra close curly brace after your 2nd constructor. Remove this - all of the code in the class needs to be within the curly braces.

public class Main {
   // all of the code in the class goes here
}

When I remove the bracket after the 2nd constructor, my methods get a different error. That is, "inner classes cannot have static declarations".

public class Main {
    class Card {
        int suit, rank;
    
        
        public Card () {
            this.suit = 0;
            this.rank = 0;
        }
        public Card (int suit, int rank) {
            this.suit = suit;
            this.rank = rank;
        }

OH! Sorry, my mistake. I didn't see that you have 2 class declarations. I'm going to take a 2nd look.

Ok, here's a better solution. Move the inner class outside of the Main class. Make it come after the first. So your structure is:

public class Main {
   // the whole Main class here
}

class Card {
   // the whole Card class here
}

So I put all of the methods in class Main, and follow it up with class Card?

Yes. Just move this code to the end, outside of the Main class.

class Card {
        int suit, rank;
        
        public Card () {
            this.suit = 0;
            this.rank = 0;
        }
        public Card (int suit, int rank) {
            this.suit = suit;
            this.rank = rank;
        }
    }

I actually think I figured it out. I just renamed the file "Card" and revised public class. So it now looks like this:

package card;

public class Card {
        int suit, rank;
        
        public Card () {
            this.suit = 0;
            this.rank = 0;
        }
        public Card (int suit, int rank) {
            this.suit = suit;
            this.rank = rank;
        }

I think this is why it wasn't public. Bleh, newb mistake.

OK. If you put the Card class in its own file and make it public, then anyone can use the Card class. This is fine if you want other classes to use it. Sometimes you really want a class to be hidden and only used by one other class. In that case you can put it at the end of the file of another class and don't call it public. You can only have one public class per file in Java.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.