title: how do I use a try/catch block on a GUI to test out methods on a Deck Class
body: <p>Hey guys running into another problem while writing a program for my Java2 class.<br>I've created a Deck() class where I define methods such as:<br>[ isEmpty(), shuffleDeck(), dealCards(), cut(), putCardsInDeck(), and finally a toString() ]<br> 
  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.<br><br>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.<br>
  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.<br>
  This is a brief description of what they are supposed to do:<br><br>
  <b>dealCards():</b> this method will remove and return the card stored at the beginning of theDeck.</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p><br>
  <b>putCardsInDeck():</b> 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.</p>
  
  <pre><code>    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 &gt; 52)
          throw new DeckException("Deck", "full");
      else
      {
          // Using current to go to the end of the deck
          for(int j=0; j&lt; ( 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 &lt; theCards.length; i++)
          {
              temp = new Node(new Card(theCards[i].getValue()), null);
              lastCard.setNext(temp);
              lastCard = lastCard.getNext();
              theCards[i] = null;
              numCards++;
          }
      }
  }
  </code></pre>
  
  <p><br><br>
  And this is the code for my DeckException class</p>
  
  <pre><code>@SuppressWarnings("serial")
  public class DeckException extends RuntimeException {
  
     public DeckException (String collection, String status)
     {
      super ("The " + collection + " is "+ status  + ".");
     }
  }
  </code></pre>
  
  <p><br><br> 
  <b>GUI code</b><br>
  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).<br><br></p>
  
  <pre><code>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 &amp;&amp; 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&lt;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
  </code></pre>
  
  <p><br><br>
  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:<br><br><b>NumberFormatException (NFE):</b> Exception in thread "main" java.lang.NumberFormatException: For input string: ""<br><br>and when I press cancel I get<br><b>NumberFormatException (NFE):</b> Exception in thread "main" java.lang.NumberFormatException: null<br><br>
  when I press the red X to close the inputdialogbox i get another<br><b>NumberFormatException (NFE):</b> Exception in thread "main" java.lang.NumberFormatException: null<br><br><b>FINALLY</b> my point is:<br>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. <br><br>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 ^_^ ) <br><br>
  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)<br><br>
  So yeah that's it. Thank You in advance.</p>