user1531959
user1531959

Reputation:

The array of strings does not display in JOptionPane

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!

Upvotes: 0

Views: 737

Answers (2)

Harmeet Singh
Harmeet Singh

Reputation: 2616

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
}

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 3

Related Questions