• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compareTo help

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic