Hi need a little help I cant see whats wrong with this code can someone take a look at this and steer me in the right direction.

Specs say: I have to make hands comparable. I only comparing hands of the same length. One hand is bigger than another if the largest card of the first hand is larger than the largest card of the second hand:

public int compareTo(Hand otherHand)
    {
        for (int i=theHand.length; i >theHand.length; i--)
        {
            if (getHand()[i]!= otherHand.getHand()[i])
                return getHand()[i].compareTo( otherHand.getHand()[i]);
            }
            return 0;
        }

In my driver this is what I have

int biggestHand = playerHand.compareTo (computerHand);
            if (biggestHand > 0)
                JOptionPane.showMessageDialog(player wins);
            else if (biggestHand < 0)
                 JOptionPane.showMessageDialog(computers wins);
            else
                JOptionPane.showMessageDialog(tie);

it keeps tieing.

apart from your compareTo method never returning anything but 0 (at least from the part you posted)?

And of course you're violating the Comparable contract by having the signature of your compareTo method take a Hand instance instead of an Object, so it's quite possible it never gets called at all.

Here's what I have now Im getting error array out of bounds

public int compareTo(Object otherObject)
{   Hand otherHand = (Hand) otherObject;
    Card biggestCardHand = getHand()[0];
    Card  biggestCardotherHand = otherHand.getHand()[0];
    
    for ( int i = 0; i <= theHand.length; i++) /*assuming you start your array with 0 */
    {
        if (biggestCardHand.compareTo(getHand()[i])< 0)
       {
         biggestCardHand = getHand()[i];
       }
       if (biggestCardotherHand.compareTo(otherHand.getHand()[i])<0)
      {
        biggestCardotherHand = otherHand.getHand()[i];
      }
    }//end loop
        if (biggestCardHand.compareTo(biggestCardotherHand)< 0)
        {
          return 1;
         }
        else
            if (biggestCardHand.compareTo(biggestCardotherHand)> 0)
       {
          return -1;            
        }
       
              
        return 0;
}//end compareTO

Use < rather than <=. I didn't really read the code, except for that bit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.