2

I have here three classes that asks for input and should display an array of strings in a dialog box.

Class 1:

public class Card {

  public int rank;
  public int suit;
  public String rankName;
  public String suitName;
  public 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;
  }
}

Class 2 - this is the class where I should output the strings I have stored in an array. I think I'm on the right track doing generics part but I'm not fond to that though. Please guide me in printing all those.

import javax.swing.JOptionPane;

public class Deck {

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

        card.setSuitName  (suitNameArray[card.getSuit()-1]);
        card.setRankName (rankNameArray[card.getRank()-1]);
        card.setCardName (card.getSuitName() + "-" + card.getRankName());
        JOptionPane.showMessageDialog(null, card.cardName);
    }

    public void displayAll (Card card) {
        for (int i=0; i<card.cardName.length(); i++) {
            String print = JOptionPane.showMessageDialog(null, card.cardName[i]);
        }
    }
}

Class 3:

import javax.swing.JOptionPane;

public class DisplayDeck {

    public static void main(String[] args) {

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

        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(card);

    }
}

I'm trying to display the cardNames input by the user.

John Gathogo
  • 4,495
  • 3
  • 32
  • 48

2 Answers2

3
public void displayAll (Card[] cards) {
    String output = "";
    for (int i=0; i<cards.length(); i++) {
        output += cards[i].getCardName() + "\n";
    }
    JOptionPane.showMessageDialog(null, output);
}

You need an array passed into displayAll otherwise you just have one card.

Then you build the output string before you ever actually create the JOptionPane.

Brinnis
  • 906
  • 5
  • 12
  • Oh I see. Thank you so much! If I'm going to call `displayAll` in another `DisplayDeck` class, what should be the argument? Thanks –  Jul 31 '12 at 16:28
  • An array of cards. Card[] cards = new Card[**Size Here**]; cards[0] = new Card(); ... – Brinnis Aug 02 '12 at 14:23
1

Another approach hinges on Deck having a collection to which each new Card is added:

class Deck {
    private List<Card> cards = new ArrayList<Card>();
    ...
}

Were Card to override toString(),

@Override
public String toString() {
    return cardName;
}

then displayAll() becomes very simple:

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

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you so much! Does the `public String toString` belong to class `Card`? When I run the program, the `JOptionPane` displays nothing. –  Aug 01 '12 at 00:50
  • Yes, I chose to override `toString()` in `Card`. To be honest, mine initially displayed nothing also. I had neglected to `add()` each `new Card()` to the `List cards`. – trashgod Aug 01 '12 at 00:58
  • I'm sorry, I don't think I get it. I don't know which `add` you're referring to? Like this add `cards.add((Card) cards);`? I'm so sorry, I'm new in Java. But thank you! –  Aug 01 '12 at 01:02
  • Yes, `Card card = new Card(); cards.add(card)`, note the singular; if you use the generic declaration above, you shouldn't have to cast the `Card` to `add()` it. – trashgod Aug 01 '12 at 01:09
  • Thanks! Should I place those inside `assignIntegerValues`? Since Eclipse says `Duplicate local variable card`, I changed the two occurrences of card into `card1` but still, it's not working. Even if I put it inside `displayAll`. I'm so sorry and thank you. –  Aug 01 '12 at 01:35
  • I did it in the `Deck` constructor, but doing it in `assignIntegerValues()` is a reasonable alternative. – trashgod Aug 01 '12 at 04:12