• Post Reply Bookmark Topic Watch Topic
  • New Topic

compareTo help  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this code returned the following error:

Main.java:15: error: cannot find symbol
i = x.compareTo(2.2);
^
symbol: method compareTo(double)
location: variable x of type Number
1 error


Q1. why did this happen? what's wrong?
Q2. what does "cannot find symbol" mean?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Cannot find symbol" generally means that you've referred to a variable, method or class that the compiler doesn't recognise.

In this case, have a look at the documentation for java.lang.Number. Can you see a compareTo() method there? java.lang.Integer has one, but Number doesn't.
 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but in this page
http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
it says the Number class has compareTo methods.

if i'm reading this wrong, can you give me an example code for how compareTo method is implemented?
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That page doesn't actually say that the Number class has any compareTo() methods. It does say that all subclasses of Number has those methods, so probably that misled you into believing that Number did as well. I agree that it's a bit confusing, especially the part which describes Integer and so on as "Number classes".

Anyway, if you want some working code like yours which uses the compareTo method, how about this?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!