0

These classes' final work when compiled asks for input and should display an array of strings in a dialog box.

1st Class

public class Card {

    private int rank;
    private int suit;
    private String rankName;
    private String suitName;
    private String cardName;

    public Card () {
        rank = 0;
        suit = 0;
        rankName = "";
        suitName = "";
        cardName = "";
    }

    public int getRank() {
        return rank;
    }   
    public void setRank(int rank) {
        this.rank = rank;
    }
    public int getSuit() {
        return suit;
    }
    public void setSuit(int suit) {
        this.suit = suit;
    }
    public String getRankName() {
        return rankName;
    }
    public void setRankName(String rankName) {
        this.rankName = rankName;
    }
    public String getSuitName() {
        return suitName;
    }
    public void setSuitName(String suitName) {
        this.suitName = suitName;
    }
    public String getCardName() {
        return cardName;
    }
    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    public String toString() {
        return cardName;
    }

}



2nd class. I thank trashgod for this one. However, here, is where is problem is. It does not display the array of strings in the dialog box. It displays nothing.

import java.util.*;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Deck {

    public List<Card> cards = new ArrayList<Card>();

    public void assignIntegerValues(Card card) {
        String[] suitNameArray = {"Clubs", "Spades", "Hearts", "Diamonds"};
        String[] rankNameArray = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

        card.setSuitName  (suitNameArray[card.getSuit()-1]);
        card.setRankName (rankNameArray[card.getRank()-1]);
        card.setCardName (card.getSuitName() + "-" + card.getRankName());
        JOptionPane.showMessageDialog(null, card.getCardName());
        Card card1 = new Card(); 
        cards.add(card1);
    }

    public void displayAll() {
        JOptionPane.showMessageDialog(null, new JScrollPane(new JList(cards.toArray())));
    }


}



3rd Class

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

public class DisplayDeck {

    public static List<Card> cards = new ArrayList<Card>();

    public static void main(String[] args) {

        Card card = new Card();
        Deck deck = new Deck();
        int answer;
        String again;
        String[] nameOfCards;

        do {
            String suitString = JOptionPane.showInputDialog("Suit: ");
            card.setSuit (Integer.parseInt(suitString));

            String rankString = JOptionPane.showInputDialog("Rank: ");
            card.setRank (Integer.parseInt(rankString)); 

            deck.assignIntegerValues(card);
            String answerString = JOptionPane.showInputDialog("Try again? (1/0) ");
            answer = Integer.parseInt(answerString);

        } while (answer == 1);

        deck.displayAll();

    }
}

Thank you very much!

Mansfield
  • 14,445
  • 18
  • 76
  • 112
  • Yes of course it's homework. @Gabby: please be upfront about homework questions and use the homework tag. – Hovercraft Full Of Eels Aug 01 '12 at 02:24
  • Thanks guys. Actually it's not a "homework". It was just given for us to practice in our upcoming exam. –  Aug 01 '12 at 02:26
  • @Gabby: that's just semantics and this is still homework -- school work that you do at home. Again, please be upfront about it. This is work that you should do the brunt of yourself in order to learn the most from it. – Hovercraft Full Of Eels Aug 01 '12 at 02:27
  • See also this [previous question](http://stackoverflow.com/q/11743861/230513) on the same topic. – trashgod Aug 01 '12 at 04:17

2 Answers2

3

The problem has nothing to do with an array (or better an ArrayList) not displaying. The problem is that the List has no useful information in it to begin with, and so there's nothing really to display.

The code is just doing what you wrote. You're not adding any card with any meaningful information into your deck. Look at where you add anything to the Deck's List<Card>:

Card card1 = new Card(); 
cards.add(card1);

You're adding a default Card object each time with no meaningful information added. All the meaningful information is just discarded. Since this is homework, I'll ask you to re-think how to do this better.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks sir. The "meaningful information" is in `cardName`. So I tried `cards.add(card.getcardName());` but it is wrong. I'm still not fond of Java's syntax. –  Aug 01 '12 at 02:29
  • @Gabby: no even if there is meaningful information in cardName, you discard it by creating a blank new Card object without any information in it and adding it to the list. Look and see where do you add any rank or value information to the Card object, card1, that you add to the list? Show me that bit of code (hint: it doesn't exist). What you're doing is akin to drawing a picture on a piece of paper, but then ignoring the picture and instead putting a blank piece of paper into the notebook when you're done with it. – Hovercraft Full Of Eels Aug 01 '12 at 02:30
  • I replaced the above with this `Card card1 = new Card(); cards.add(card);` and it just displays the latest rank and suit given by the user. Thanks again sir. –  Aug 01 '12 at 02:37
  • @Gabby: I think that you need to create a new Card object each time you do the loop. Otherwise your list will hold multiple references to the one single same Card object. – Hovercraft Full Of Eels Aug 01 '12 at 02:42
  • When I place those (`Card card = new Card();` and`Card card1 = new Card(); cards.add(card);`) inside the do-while, it displays nothing. –  Aug 01 '12 at 02:46
  • @Gabby: Show your latest code as an addition to your original question. Also consider just using println's until you've got the code working, so you can display the output here. – Hovercraft Full Of Eels Aug 01 '12 at 02:48
1

The problem with your code is that the card which is storing information(name) has not been used and gets discarded and as a new card(has no data) you are creating and passing to list

card.setCardName (card.getSuitName() + "-" + card.getRankName()); //hold data
Card card1 = new Card();  // holds nothing (useless)
cards.add(card1); // will have card with no data

public void displayAll() {
   JOptionPane.showMessageDialog(null, new JScrollPane(new JList(cards.toArray())));
    // has nothing to display
}
Harmeet Singh
  • 2,555
  • 18
  • 21
  • Thank you! I just used this instead `cards.add(card);` and it now works. :) –  Aug 01 '12 at 03:03