Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Card Description Program in Scanner

    ok, using a standard deck of 42 cards, I'm supposed to make a program that is supposed to take in a user input of a card name in shorthand notation (i.e. 4S) and the program is supposed to give the description of the card (i.e. if the above example were inputted, the output would be Four of Spades)

    the problem I'm having is that whatever I input comes out as nullSpades (if using the above example) instead of the proper description

    class:
    Java Code:
     
      public class Card 
       {
       
          String cardNumber,cardSuit;
       
          public Card(String description)
          {
             String cardDescription = description;
             String cardNumberValue = cardDescription.substring(0);
                      	
             if (cardNumberValue.equals("1"))
             {
                cardNumber = "Ten of ";
             }
                
             else if (cardNumberValue.equals("2"))
             {
                cardNumber = "Two of ";
             }
             
             else if (cardNumberValue.equals("3"))
             {
                cardNumber = "Three of ";
             }
             
             else if (cardNumberValue.equals("4"))
             {
                cardNumber = "Four of ";
             }
             	
             else if (cardNumberValue.equals("5"))
             {
                cardNumber = "Five of ";
             }
             	
             else if (cardNumberValue.equals("6"))
             {
                cardNumber = "Six of ";
             }
             	
             else if (cardNumberValue.equals("7"))
             {
                cardNumber = "Seven of ";
             }
             	
             else if (cardNumberValue.equals("8"))
             {
                cardNumber = "Eight of ";
             }
             	
             else if (cardNumberValue.equals("9"))
             {
                cardNumber = "Nine of ";
             }
             	
             else if (cardNumberValue.equalsIgnoreCase("J"))
             {
                cardNumber = "Jack of ";
             }
             	
             else if (cardNumberValue.equalsIgnoreCase("Q"))
             {
                cardNumber = "Queen of ";
             }
             	
             else if (cardNumberValue.equalsIgnoreCase("K"))
             {
                cardNumber = "King of ";
             }
             
             else if (cardNumberValue.equalsIgnoreCase("A"))
             {
                cardNumber = "Ace of ";
             }
                
             String cardSuitValue = cardDescription.substring(1);
          		
             if (cardSuitValue.equalsIgnoreCase("D"))
             {
                cardSuit = "Diamonds";
             }
             	
             else if (cardSuitValue.equalsIgnoreCase("H"))
             {
                cardSuit = "Hearts";
             }
             	
             else if (cardSuitValue.equalsIgnoreCase("S"))
             {
                cardSuit = "Spades";
             }
             
             else if (cardSuitValue.equalsIgnoreCase("C"))
             {
                cardSuit = "Clubs";
             }
          }
          
          public String getDescription()
          {
             String fullDescription = cardNumber + cardSuit;
             return fullDescription;
          }
       }
    driver:
    Java Code:
       import java.util.Scanner;
    
       public class CardDriver 
       {
          public static void main(String[] args) 
          {
             Scanner reader = new Scanner(System.in);
           
             System.out.print("Enter a card number and its suit like this (4S): ");
             
             String cardDescription = reader.next();
             String description = cardDescription;
           
             Card card = new Card(cardDescription);
           
             System.out.println(card.getDescription());
          }
       }
    Last edited by sauronamaterasu13; 09-14-2012 at 12:35 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Card Description Program in Scanner

    To see what the code is doing, use a println() statement to print out the value of variables like Number, cardNumber and userInput after they are given values.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Card Description Program in Scanner

    ok, i got the if else around the bottom of the class to print out the suit, but the problem now is that whenever i try to get the number of the card, it prints out null

  4. #4
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Card Description Program in Scanner

    You are using the substring method wrongly. The way you are calling it is only giving the same string.

    You need to give a range when you want to have specific character.
    Like this
    Java Code:
    String cardNumberValue = cardDescription.substring(0, 1);
    or
    Java Code:
    String cardNumberValue = Character.toString(cardDescription.charAt(0));

  5. #5
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Card Description Program in Scanner

    ok i gotcha, but how come it ends up working for the suit and not the number

    because i have this code for number:

    Java Code:
    String cardNumberValue = cardDescription.substring(0);
    and this for the suit:

    Java Code:
    String cardNumberValue = cardDescription.substring(1);
    Last edited by sauronamaterasu13; 09-14-2012 at 01:36 AM.

  6. #6
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: Card Description Program in Scanner

    Exactly.
    Here's the JavaDoc comment for the version of substring you use.
    "The substring begins with the character at the specified index and extends to the end of this string."

  7. #7
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Card Description Program in Scanner

    o ok, i see what you are taking about, thanks

Similar Threads

  1. Replies: 4
    Last Post: 06-15-2012, 07:10 AM
  2. Replies: 1
    Last Post: 06-07-2012, 03:49 AM
  3. java card program
    By firdose in forum New To Java
    Replies: 5
    Last Post: 02-14-2012, 02:51 PM
  4. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 11:11 AM
  5. Card program , need help thanks.
    By carlos123 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •