Results 1 to 5 of 5
- 01-31-2010, 10:46 AM #1
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
this is the line thats giving me trouble jcreator :confused: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
thanks in advance for the helpJava Code:return this.getBoyNumber().compareToIgnoreCase(boyToCompare.getBoyNumber());
- 01-31-2010, 11:45 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 01-31-2010, 12:11 PM #3
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 ?"
- 02-02-2010, 12:27 AM #4
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.
- 02-02-2010, 02:24 AM #5
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
-
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
Create code for Binary Search Tree using compareTo method using I/O
By harvin23 in forum New To JavaReplies: 5Last Post: 10-13-2009, 01:35 PM -
[SOLVED] Sorting / compareTo method confusion. Please help
By kbullard516 in forum New To JavaReplies: 8Last Post: 03-19-2009, 09:38 PM -
compareTo()
By Tsiliadoros in forum Advanced JavaReplies: 5Last Post: 10-03-2008, 01:18 PM -
how compareTo Method works
By nanaji in forum Advanced JavaReplies: 1Last Post: 06-22-2008, 07:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks