Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Card and CardTest

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Lightbulb Card and CardTest

    hi i have a logic error with my program. it returns and prints unknown for every value. have a look...
    what should i even put in the constructor?

    public class Card 
    {
    	 public Card()
    	  {
    	    String value, suit;
    	  }
     
    	  /* getDescription method that takes
    	   * the user input and turns it into a longer string
    	   * or returns the string, "Unknown"
    	   * */
    	  public String getDescription(String value)
    	  {
    	    String cardValue = value.substring(0,1);    //breaking apart the suit from the value using the substring method
    	    if (cardValue.equalsIgnoreCase("A"))
    	      value = "Ace of ";
    	    else if (cardValue.equals("2"))
    	      value = "Two of ";
    	    else if (cardValue.equals("3"))
    	      value = "Three of ";
    	    else if (cardValue.equals("4"))
    	      value = "Four of ";
    	    else if (cardValue.equals("5"))
    	      value = "Five of ";
    	    else if (cardValue.equals("6"))
    	      value = "Six of ";
    	    else if (cardValue.equals("7"))
    	      value = "Seven of ";
    	    else if (cardValue.equals("8"))
    	      value = "Eight of ";
    	    else if (cardValue.equals("9"))
    	      value = "Nine of ";
    	    else if (cardValue.equals("1"))  //only concerned about the first digit, so 1 will suffice for ten!
    	      value = "Ten of ";
    	    else if (cardValue.equalsIgnoreCase("J")) //using the method equalsIgnoreCase() to ensure reading the user's data properly
    	      value = "Jack of ";
    	    else if (cardValue.equalsIgnoreCase("Q"))
    	      value = "Queen of ";
    	    else if (cardValue.equalsIgnoreCase("K"))
    	      value = "King of ";
    	    else
    	      return "Unknown";
    	    //end if
    	    //now I handle the problem of having the card 10 in the user input with the following if statement
    	    String suit;  //string that will return the suit INTO the return value
    	    if (cardValue.equals("1"))
    	          suit = value.substring(2,3);
    	    else 
    	          suit = value.substring(1,2);
    	    //end if
     
    	    //now this if statement discovers the suit
    	    if (suit.equalsIgnoreCase("D"))
    	         suit = "Diamonds";
    	    else if (suit.equalsIgnoreCase("H"))
    	         suit = "Hearts";
    	    else if (suit.equalsIgnoreCase("S"))
    	         suit = "Spades";
    	    else if (suit.equalsIgnoreCase("C"))
    	         suit = "Clubs";
    	    else
    	          return "Unknown";
    	    //end if
     
    	    String fullDescription = value + suit;
    	    return fullDescription;
    	  }
    }
     
    public class CardTest 
    {
    	/**
    	 * this program runs the card class and prints a description of the user's card
    	 */
    	public static void main(String[] args) 
    	{
    	    Scanner in = new Scanner(System.in);
    	    Card card = new Card();
    	    System.out.println("Enter a card and its suit like this (4S): ");
    	    String userInput = in.next();
    	    System.out.println(card.getDescription(userInput));
    	}
    }
    it prints unknown on every run!
    Last edited by helloworld922; January 29th, 2010 at 06:20 PM. Reason: please use [code] tags!


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Card and CardTest

    Your getDescription method has a String parameter called value, and this hides the instance variable value. You should probably set 'value' in the constructor, and remove the parameter from getDescription. Something like:
    public class Card
    {
    	String description;
     
    	public Card(String d)
    	{
    		description = d;
    	}
     
     
    	public String getDescription()
    	{
    		//Convert description to long form
    		//with your existing code, and return
    	}
     
    }

    It would probably actually be better if the constructor called a method to convert description to the long form you want. That way it only has to be done once, and getDescription can just return the string:
    public class Card
    {
    	String description;
     
    	public Card(String d)
    	{
    		description = Convert(d);
    	}
     
    	private String Convert(String d)
    	{
    		//All the messy stuff
    	}
     
    	public String getDescription()
    	{
    		return description;
    	}
     
    }

    The method Convert can have temporary variables called value and suit if you like.

Similar Threads

  1. my programs- Card and CardTest
    By etidd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2010, 08:06 PM
  2. Method for Cedit card check, help!!!!1
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 31st, 2009, 09:16 AM