Hey guys running into another problem while writing a program for my Java2 class.
I've created a Deck() class where I define methods such as:
[ isEmpty(), shuffleDeck(), dealCards(), cut(), putCardsInDeck(), and finally a toString() ]
I've written code for all of them and so far they are working perfectly thanks to some users that have help me out on this site.
Anyways I'm trying create an application class in which it tests out several of my methods on a deck. I know I can easily just call on methods print them out and hard code most of the other things to test out these methods. But I want to go beyond that.
I'm in the middle of creating a GUI that allows the user to test these methods by pressing buttons and here's where I run into a little problem with the dealCards() and putCardsInDeck() methods.
This is a brief description of what they are supposed to do:
dealCards(): this method will remove and return the card stored at the beginning of theDeck.
public String dealCard()
{
Node current;
Card cTemp = null;
if(isEmpty())
throw new DeckException("theDeck","empty");
else
{
current = theDeck;
cTemp = current.getItem();
theDeck = theDeck.getNext();
numCards--;
}
return cTemp.toString();
}
putCardsInDeck(): this will accept a reference to a Card array. The cards in the array are to be removed and stored in the deck, leaving the array empty. The cards removed from the array should be stored at the bottom of the deck. If the number of cards in the array will cause the deck size to become greater than 52 a DeckException should be thrown and should contain an appropriate message.
public void putCardsInDeck( Card [] cards ) throws DeckException
{
Card[] theCards = cards;
Node current = theDeck;
Node lastCard = theDeck;
Node temp;
int totalCards = theCards.length + numCards;
if (totalCards > 52)
throw new DeckException("Deck", "full");
else
{
// Using current to go to the end of the deck
for(int j=0; j< ( numCards - 1 ); j++)
current = current.getNext();
// Storing the where current landed in lastCard
lastCard = current;
// Using a for loop to go thru the array and put the cards after the lastCard
for (int i=0; i < theCards.length; i++)
{
temp = new Node(new Card(theCards[i].getValue()), null);
lastCard.setNext(temp);
lastCard = lastCard.getNext();
theCards[i] = null;
numCards++;
}
}
}
And this is the code for my DeckException class
@SuppressWarnings("serial")
public class DeckException extends RuntimeException {
public DeckException (String collection, String status)
{
super ("The " + collection + " is "+ status + ".");
}
}
GUI code
Ok so now to the part that is giving me problems.
In the following code I make use of a GUI that executes methods on a deck (I haven't finished the: putCardsInDeck (haven't finished this because I think I will run into the same problem as I am with the DealCards method) or new deck button yet btw (you'll see).
public static void main (String [] args)
{
// Creating a new Deck
Deck theDeck = new Deck();
String listOfCards = "";
int numCardsDealt;
String[] choices = {"Display Deck", "Shuffle Deck", "Cut Deck", "Deal Deck","PutCardsInDeck", "New Deck", "Exit"};
int choice = JOptionPane.showOptionDialog(null,
"Enter your choice....",
"Main Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
while(choice !=6 && choice !=-1)
{
//try{
switch(choice)
{
// Just calls theDeck.toString() and displays it on a scrollpane
case 0:
JTextArea text = new JTextArea(20,20);
JScrollPane scroll = new JScrollPane(text);
text.setText(theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"The Deck",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck before the deck is shuffled and then after
case 1:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before shuffleDeck method:\n" + theDeck.toString());
theDeck.shuffleDeck();
text.append("\n\nTheDeck after shuffleDeck method:\n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"Testing the shuffleDeck() method on theDeck.",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck object before the cut method and after
case 2:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before cut method: \n" + theDeck.toString());
theDeck.cut();
text.append("\n\nTheDeck after cut method: \n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"Testing out the cut() method on theDeck",
JOptionPane.DEFAULT_OPTION);
break;
// Displays the status of theDeck before cards are dealt
// Asks user to select a number of cards and deals that amount of cards
// Displays the status of theDeck after the cards have been dealt
case 3:
text = new JTextArea(20,20);
scroll = new JScrollPane(text);
text.setText("TheDeck before dealCard method: \n" + theDeck.toString());
numCardsDealt = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of cards you want dealt..."));
try
{
for( int i = 0; i<numCardsDealt; i++)
listOfCards+="Card dealt " + (i+1) + ": " +theDeck.dealCard() + "\n";
text.append("\n\nList of cards dealt:\n" + listOfCards);
text.append("\n\nTheDeck after dealCard method: \n" + theDeck.toString());
JOptionPane.showMessageDialog(
null,
scroll,
"The Deck before its been cut.",
JOptionPane.DEFAULT_OPTION);
}
catch(DeckException ex)
{
JOptionPane.showMessageDialog(null, "There are not enough cards in \nthe deck" +
"to satisty the number\nof cards you have entered to be dealt.");// \n"Please try a different number");
}
break;
} // ends switch
choice = JOptionPane.showOptionDialog(null,
"Enter your choice....",
"Main Menu",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
//} // ends try
//catch(NullPointerException ex)
//{
//System.out.println("NullPointerException");
//}
//catch(NumberFormatException ex)
//{
//System.out.println("NumberFormatException");
//}
}// ends while loop
When the my application class runs and the user clicks on the DealCard button, the user is asked to enter a number but let's say the user doesn't enter a number I get:
NumberFormatException (NFE): Exception in thread "main" java.lang.NumberFormatException: For input string: ""
and when I press cancel I get
NumberFormatException (NFE): Exception in thread "main" java.lang.NumberFormatException: null
when I press the red X to close the inputdialogbox i get another
NumberFormatException (NFE): Exception in thread "main" java.lang.NumberFormatException: null
FINALLY my point is:
I want to be able to compensate for these situations and if the user hasn't entered anything the pop up should keep coming back asking the user to enter a number until they press the red X or cancel.
but when I use the try/catch block the inputdialogbox keeps coming back regardless if i press the red X, don't input anything or press cancel. =/ ( My other idea was to generate a random number if the user leaves the inputdialogbox empty...but one step at a time ^_^ )
P.S. You will see in my code that I have another try/catch block in case 3 that is there to catch another exception so as to prevent the user from dealing more cards than there are in the deck. ( I think I would have to make another method called size() to see how many cards are inside the deck but IDK... what do you guys think)
So yeah that's it. Thank You in advance.
*So I managed to figure it out and so far it works.
String parseThis;
do {
parseThis = JOptionPane.showInputDialog(null, "Please enter the number of cards you want dealt...");
} while (parseThis == "");
if (parseThis == null)
break;
numCardsDealt = Integer.parseInt(parseThis);
This will make the inputdialogbox keep coming back if the user does not insert a value. it will disappear once the user presses cancel or closes the inputdialogbox
Thanks anyways