Results 1 to 5 of 5
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation need help wit this compareTo method

    cant figure out this int cannot be deferenced error
    it wont let me compile the code what im i doing wrong
    does the compare to only work for string objects only instead of ints
    any help would be much appreciated here my code
    Java Code:
    public class Boy implements Comparable <Boy> 
    {
    	//attributes
    	private String boyName;
    	private int boyNumber;
    	
    	//constructor
    	public Boy(String boyName,int boyNumber)
    	{
    		setBoyName(boyName);
    		setBoyNumber(boyNumber);
    	}
    	public Boy(Boy theObject)
    	{
    		this.setBoyNumber(this.getBoyNumber());
    	}	
    	public Boy()
    	{
    		
    	}
    	//get set methods
    	//------------------------------------
    	public void setBoyName(String boyName)
    	{
    		this.boyName = boyName;
    	}
    	public String getBoyName()
    	{
    		return this.boyName;
    	}
    	//---------------------------------------
    	public void setBoyNumber(int boyNumber)
    	{
    		this.boyNumber = boyNumber;
    	}
    	public int getBoyNumber()
    	{
    		return this.boyNumber;
    	}
    	//---------------------------------------
    	/*public boolean equals(Object theObject)
    	{
    	
    	}*/	
    	
    	//---------------------------------------
    	public String toString()
    	{
    		return String.format("%s , %d\n",getBoyName(),getBoyNumber());
    	}
    	//-----------------------------------------
    	public int compareTo(Boy boyToCompare)//for search method
    	{
    		int compareClass = this.getClass().getName().compareToIgnoreCase(boyToCompare.getClass().getName());
    		
    		if(compareClass == 0)
    		{
    			return this.getBoyNumber().compareToIgnoreCase(boyToCompare.getBoyNumber());
    		}
    		else
    		{
    			return compareClass;
    		}		
    	}							
    }//end of boy class
    this is the line thats giving me trouble jcreator :confused:
    Java Code:
    return this.getBoyNumber().compareToIgnoreCase(boyToCompare.getBoyNumber());
    thanks in advance for the help

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ShinTec View Post
    this is the line thats giving me trouble jcreator :confused:
    Java Code:
    return this.getBoyNumber().compareToIgnoreCase(boyToCompare.getBoyNumber());
    thanks in advance for the help
    The getBoyNumber() method returns an 'int'. Ints are primitive types and don't have any methods; therefore you can't dereference ints, i.e. they don't have an compareToIgnoreCase( ... ) method (nor any other method).

    kind regards,

    Jos

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    you cannto compare elemntar types like 'int' (that your boynumber) with a compareToIgnoreCase. That even does not makes sense.

    But you can compareTo a boy object with anthoer object.
    and you can comapare boyName with compareToIgnoreCase with another string
    and you can comapre boyNumber with '==' operator
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  4. #4
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Talking

    i cant believe i didnt pick those things up cheers for that josAh and andreB :D
    Last edited by ShinTec; 02-02-2010 at 12:32 AM.

  5. #5
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    omg, i realized how many writing errors i have in my post :-)
    shame on me
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  2. Replies: 5
    Last Post: 10-13-2009, 01:35 PM
  3. Replies: 8
    Last Post: 03-19-2009, 09:38 PM
  4. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 01:18 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07:40 PM

Posting Permissions

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