Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compareTo() HELP!

Status
Not open for further replies.

ilovelinux2006

Programmer
Jun 6, 2006
32
US
Hey everyone,

Ive been working on this for hours!
I have three variables: a, b, c


I am suppose to use a.compareTo(b)

Here is the function:

public int compareTo(Number t){
if(t.getNumber() > this.getNumber()){
return 1;
}else if(t.getNumber() < this.getNumber()){
return -1;
}else{
return 0;
}
}

I have been working many many many hours on trying to get the middle number!!!!!!!!!! I found how to get the two largest and smallest numbers with the compareTo() function:

// Returns the smallest of the 3 Numbers:

if(b.compareTo(a) <= 0 && c.compareTo(a) <=0){

System.out.print(a);
}else if(a.compareTo(b) <=0 && c.compareTo(b) <=0){
System.out.print(b);
}else{
System.out.print(c);
}

// Returns the largest of the 3 Numbers: (I just changed the < to >)

if(b.compareTo(a) >= 0 && c.compareTo(a) >=0){

System.out.print(a);
}else if(a.compareTo(b) >=0 && c.compareTo(b) >=0){
System.out.print(b);
}else{
System.out.print(c);
}


Please help me, this is making me go crazy!
 
Sort the numbers, then go to the middle of the array.
 
I am suppose to use a.compareTo(b)

Forgive me if I'm wrong, but that phrase makes it sound like this is a homework assignment or something. Is It?

Tim
 
You got the big one, you got the smaller one: the middle one is the one left :)

Cheers,
Dian
 
left of the middle or left of the right one. Can't be left of the left since there are only three elements.

Christiaan Baes
Belgium

"My new site" - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top