Results 1 to 7 of 7
- 09-12-2012, 11:40 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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:
driver: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; } }
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-13-2012 at 11:35 PM.
- 09-13-2012, 01:50 AM #2
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.
- 09-13-2012, 11:37 PM #3
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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
- 09-14-2012, 12:13 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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
orJava Code:String cardNumberValue = cardDescription.substring(0, 1);
Java Code:String cardNumberValue = Character.toString(cardDescription.charAt(0));
- 09-14-2012, 12:34 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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:
and this for the suit:Java Code:String cardNumberValue = cardDescription.substring(0);
Java Code:String cardNumberValue = cardDescription.substring(1);
Last edited by sauronamaterasu13; 09-14-2012 at 12:36 AM.
- 09-14-2012, 12:41 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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."
- 09-14-2012, 12:55 AM #7
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Bi-directional card reading Reads up to two tracks of card data DES/TDES Encryption
By mageshge21 in forum Advanced JavaReplies: 4Last Post: 06-15-2012, 06:10 AM -
I do not know what i am doing wrong with my library card program
By dloexcitement in forum New To JavaReplies: 1Last Post: 06-07-2012, 02:49 AM -
java card program
By firdose in forum New To JavaReplies: 5Last Post: 02-14-2012, 01:51 PM -
Question Card Layout, Card Management
By lrichil in forum AWT / SwingReplies: 1Last Post: 04-22-2010, 10:11 AM -
Card program , need help thanks.
By carlos123 in forum New To JavaReplies: 2Last Post: 12-31-2007, 07:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks