6 Replies - 526 Views - Last Post: 21 February 2012 - 09:34 PM Rate Topic: -----

#1 apcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 12

Java can't find symbol when I try to compile?

Posted 21 February 2012 - 04:57 PM

When I try to compile, I get these two errors and I don't know how to fix the problem. It's working for the other methods, so I don't understand what's wrong with these two..

Errors:
Hand.java:36: error: cannot find symbol
if (e.getSuit().equals(theSuit))
^
symbol: method getSuit()
location: variable e of type Card
Hand.java:49: error: cannot find symbol
if (e.compareTo(min)<0)
^
symbol: method compareTo(Card)
location: variable e of type Card

import java.util.ArrayList;

public class Hand {

private ArrayList<Card> z;
    public Hand()
    {
    z= new ArrayList <Card>();
    }


    //adds a card to the Hand.
  public void add( Card xCard)
   {
    z.add(xCard);
   }


   //returns true if card is in hand, otherwise returns false
public boolean find(Card xCard)
{
 for (Card e: z)
 { if (xCard== e)
   return true;
 }
    return false;
}


//returns the number of cards in the hand that match the suit passed.
public int numCards(String theSuit)
{
 int matches=0;
 for (Card e: z)
 {
  if (e.getSuit().equals(theSuit))
   matches++;
 }
 return matches;
}


//returns the card with the lowest card value.
public Card smallestValue()
{
    Card min= z.get(0);
  for (Card e: z)
 {
 	if (e.compareTo(min)<0)
    min=e;
 }
   return min;
}


//returns a string containing all the cards in the hand in a readable format.
public String toString()
{
 String a = "";
 for (Card e: z)
 {
   a +=e.toString() + "\n";
 }
 return a;
}


}



Is This A Good Question/Topic? 0
  • +

Replies To: Java can't find symbol when I try to compile?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 05:03 PM

Where is the card class? is it comparable? does it has a method compareTo()?
Also is getSuit() public in your Card class?
Was This Post Helpful? 0
  • +
  • -

#3 apcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 12

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 05:08 PM

This is my Card class, and yes it's comparable:
public class Card {
 private String cardType;
    public Card(String cardName){
     cardType = cardName;
    }

    public String getDescription()
    {
     String a = "Ace";
     String j = "Jack";
     String q = "Queen";
     String k = "King";
     String ten = "Ten";
     String two = "Two";
     String three = "Three";
     String four = "Four";
     String five = "Five";
     String six = "Six";
     String seven = "Seven";
     String eight = "Eight";
     String nine = "Nine";
     String spade = "Spade";
     String heart = "Heart";
     String club = "Club";
     String diamond = "Diamond";
     String message = "";
     String suit = "";

     if(cardType.charAt(0)==('A'))
     {
      message = "Ace of ";
     }
     if(cardType.charAt(0)==('J'))
     {
      message = "Jack of ";
     }
     if(cardType.charAt(0)==('Q'))
     {
      message = "Queen of ";
     }
     if(cardType.charAt(0)==('K'))
     {
      message = "King of ";
     }
     if(cardType.charAt(0)=='1')
     {
      message = "Ten of ";
     }
     if(cardType.charAt(0)=='2')
     {
      message = "Two of ";
     }
     if(cardType.charAt(0)=='3')
     {
      message = "Three of ";
     }
     if(cardType.charAt(0)=='4')
     {
      message = "Four of ";
     }
     if(cardType.charAt(0)=='5')
     {
      message = "Five of";
     }
     if(cardType.charAt(0)=='6')
     {
      message = "Six of ";
     }
     if(cardType.charAt(0)=='7')
     {
      message = "Seven of ";
     }
     if(cardType.charAt(0)=='8')
     {
      message = "Eight of ";
     }
     if(cardType.charAt(0)=='9')
     {
      message = "Nine of ";
     }
     //Suit check
     if(cardType.charAt(cardType.length()-1)==('S'))
     {
      suit = "Spades.";
     }
     if(cardType.charAt(cardType.length()-1)==('H'))
     {
      suit = "Hearts.";
     }
     if(cardType.charAt(cardType.length()-1)==('C'))
     {
      suit = "Clubs.";
     }
     if(cardType.charAt(1)==('D'))
     {
      suit = "Diamonds.";
     }
     return message + suit;


    }

}



And this is the tester:
public class TestHand
{
	public static void main(String[] args)
	{
			Hand h = new Hand();
			Card ac = new Card("Ace", "Clubs");
			Card as = new Card("Ace", "Spades");
			Card ad = new Card("Ace", "Diamonds");
			Card ah = new Card("Ace", "Hearts");
			Card td = new Card("Three", "Diamonds");
			Card fh = new Card("Four", "Hearts");
			h.add(fh);
			h.add(td);
			h.add(ac);
			h.add(as);
			h.add(ah);
			System.out.println(h);

			if (h.find(ad))
				System.out.println("Found " + ad);
			else
				System.out.println("Didn't find " + ad);
			if (h.find(ah))
				System.out.println("Found " + ah);
			else
				System.out.println("Didn't find " + ah);

			System.out.println("\n" + "There are " + h.numCards("Hearts") +
			                   " hearts in the hand.");

			System.out.println("\nThe smallest card value in the hand is " +
			                   h.smallestValue());
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 apcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 12

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 05:19 PM

Update: I fixed the first problem. I had to change it to getDiscription rather than getSuit. I'm still stuck on the second problem, though.

//returns the number of cards in the hand that match the suit passed.
public int numCards(String theSuit)
{
 int matches=0;
 for (Card e: z)
 {
  if (e.getDescription().equals(theSuit))
   matches++;
 }
 return matches;
}


//returns the card with the lowest card value.
public Card smallestValue()
{
    Card min= z.get(0);
  for (Card e: z)
 {
 	if (e.compareTo(min)<0)
    min=e;
 }
   return min;
}



*getDescription
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 05:25 PM

View Postapcs, on 22 February 2012 - 01:08 AM, said:

This is my Card class, and yes it's comparable:


You say that, but your code calls you a liar. There is no definition of compareTo anywhere in your Card class (nor do you implement the Comparable interface - though that's not strictly necessary).

If you don't define a compareTo method in your Card class, then you obviously can't call compareTo on Card objects.
Was This Post Helpful? 0
  • +
  • -

#6 apcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 12

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 05:30 PM

So what you're saying is I need to write a compareTo method?
but the assignment I was given told me "Given a Card class that realizes Comparable write a Hand class" so I thought that meant comparable was somehow already in the code.. I guess I thought wrong.
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1690
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: Java can't find symbol when I try to compile?

Posted 21 February 2012 - 09:34 PM

View Postapcs, on 22 February 2012 - 01:30 AM, said:

So what you're saying is I need to write a compareTo method?


Yes

Quote

but the assignment I was given told me "Given a Card class that realizes Comparable write a Hand class" so I thought that meant comparable was somehow already in the code..


The text you quote certainly makes it sound that way. But the code you've shown definitely does not implement Comparable. So either the assignment is misleadingly phrased or you somehow grabbed the wrong version of the code for the Card class or something.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1