5 Replies Latest reply: Jan 18, 2007 3:15 PM by 807607 RSS

    Extracting Characters

    807607
      For my course, we have to make a program that calls JOptionPane.showInputDialog to get a user input of a card value (A, 3, 5 ,10, Q) and a suit type (C, D, H, S). I was wondering what the best way would be to get around this problem. My code so far is below, and it won't work because the '0' in "10" gets extracted instead of the suit because it's a 2-digit number.
      public class Card
      {
          public Card()
          {
              card = ' ';
              suit = ' ';
          }
          
          public Card(String input1)
          {
              String input = input1.toUpperCase();
              card = input.charAt(0);
              suit = input.charAt(1);
          }
          
          public String getDescription()
          {
              String cardO = null;
              switch (card)
              {
                  case 'A': cardO = "Ace";
                      break;
                  case '2': cardO = "Two";
                      break;
                  case '3': cardO = "Three";
                      break;
                  case '4': cardO = "Four";
                      break;
                  case '5': cardO = "Five";
                      break;
                  case '6': cardO = "Six";
                      break;
                  case '7': cardO = "Seven";
                      break;
                  case '8': cardO = "Eight";
                      break;
                  case '9': cardO = "Nine";
                      break;
                  case '1': cardO = "Ten";
                      break;
                  case 'J': cardO = "Jack";
                      break;
                  case 'Q': cardO = "Queen";
                      break;
                  case 'K': cardO = "King";
                      break;
                  default: cardO = "Not a valid card value";
                      break;
              }
              String suitO = null;
              switch (suit)
              {
                  case 'D': suitO = " of Diamonds";
                      break;
                  case 'H': suitO = " of Hearts";
                      break;
                  case 'S': suitO = " of Spades";
                      break;
                  case 'C': suitO = " of Clubs";
                      break;
                  default: suitO = " of a non-valid suit type";
                      break;
              }
              return cardO + suitO;
          }
          
          private char card;
          private char suit;
      }
      Message was edited by:
      somody
        • 1. Re: Extracting Characters
          807607
          For my course, we have to make a program that calls
          JOptionPane.showInputDialog to get a user input of a
          card value (A, 3, 5 ,10, Q) and a suit type (C, D, H,
          S). I was wondering what the best way would be to
          get around this problem.
          http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
          My code so far is below,
          and it won't work because the '0' in "10" gets
          extracted instead of the suit because it's a 2-digit
          number.
          So take the two inputs separately.
          • 2. Re: Extracting Characters
            807607
            Unfortunately, it has to be taken together.
            • 3. Re: Extracting Characters
              807607
              Unfortunately, it has to be taken together.
              Well then make them input the suit first since it's always 1 char. Then the rest of it is rank.
              • 4. Re: Extracting Characters
                807607
                Option 1. Require user input to place a space (value'space'suit) and then use split to get the two values.

                Option 2. If this is not allowed then look at the length of the string. If it is 3 chars long then value is first two chars and suit is 3rd char else first char is value and second char is suit.

                Just thought of anthor

                Option 3. Since 10 is the only special case you can use startsWith("1") then proceed as above.

                Message was edited by:
                flounder
                • 5. Re: Extracting Characters
                  807607
                  Excellent. I ended up using flounder's second suggestion. Thanks to everybody. Here's my final code.
                  public class Card
                  {
                      public Card()
                      {
                          card = ' ';
                          suit = ' ';
                      }
                      
                      public Card(String input1)
                      {
                          String input = input1.toUpperCase();
                          card = input.charAt(0);
                          if (input.length() == 3)
                              suit = input.charAt(2);
                          else suit = input.charAt(1);
                      }
                      
                      public String getDescription()
                      {
                          String cardO = null;
                          switch (card)
                          {
                              case 'A': cardO = "Ace";
                                  break;
                              case '2': cardO = "Two";
                                  break;
                              case '3': cardO = "Three";
                                  break;
                              case '4': cardO = "Four";
                                  break;
                              case '5': cardO = "Five";
                                  break;
                              case '6': cardO = "Six";
                                  break;
                              case '7': cardO = "Seven";
                                  break;
                              case '8': cardO = "Eight";
                                  break;
                              case '9': cardO = "Nine";
                                  break;
                              case '1': cardO = "Ten";
                                  break;
                              case 'J': cardO = "Jack";
                                  break;
                              case 'Q': cardO = "Queen";
                                  break;
                              case 'K': cardO = "King";
                                  break;
                              default: cardO = "Not a valid card value";
                                  break;
                          }
                          String suitO = null;
                          switch (suit)
                          {
                              case 'D': suitO = " of Diamonds";
                                  break;
                              case 'H': suitO = " of Hearts";
                                  break;
                              case 'S': suitO = " of Spades";
                                  break;
                              case 'C': suitO = " of Clubs";
                                  break;
                              default: suitO = " of a non-valid suit type";
                                  break;
                          }
                          return cardO + suitO;
                      }
                      
                      private char card;
                      private char suit;
                  }