Results 1 to 5 of 5

Thread: compareTo.float

  1. #1
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default compareTo.float

    I am having difficulty with a sorting routine. I believe that the concept is valid (although not necessarily the most efficient), but I keep running into a problem. I am trying to use the compareTo function to identify the relationship between two values in an array, but it seems to have an issue with it being a comparison of two float values.
    Java Code:
    for (int x = 0; x < 430; x++)
          {
            for (int y = 0; y < 430; y++)
            {
              if (dataArray[y].compareTo(dataArray[y + 1]) > 0);
              {
                tempOpen = dataArray[y];
                dataArray[y] = dataArray[y + 1];
                dataArray[y + 1] = tempOpen;
                
                tempDate = dateArray[y];
                dateArray[y] = dateArray [y + 1];
                dateArray [y + 1] = tempDate;
              }
            }
          }
    gives the compile error as follows:

    File: C:\Users\Brad\Downloads\Assignment 3\Calculations.java [line: 157]
    Error: Cannot invoke compareTo(float[]) on the array type float[]

    Any help would be appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: compareTo.float

    Aren't your arrays 2D? Thats what compareTo is complaining about. You are passing an instance of a 1D array.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: compareTo.float

    Thanks Jim. Not sure how I missed that. Unfortunately now it is giving me a slightly different error

    Cannot invoke compareTo(float) on the primitive type float

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: compareTo.float

    Primitives (such as floats) don't have methods; you can and have to compare them with the ordinary arithmetic operators.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: compareTo.float

    Look at the Arrays class. It has methods for sorting arrays of primitives.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. compareTo method
    By Tabula Rasa in forum New To Java
    Replies: 4
    Last Post: 04-21-2011, 05:49 PM
  2. compareTo() problems...
    By falkon114 in forum New To Java
    Replies: 1
    Last Post: 01-28-2011, 10:08 PM
  3. using the compareTo method
    By anonymous445 in forum New To Java
    Replies: 3
    Last Post: 01-19-2011, 04:53 AM
  4. Help with interfaces, compareTo etc
    By jigglywiggly in forum New To Java
    Replies: 10
    Last Post: 09-07-2009, 02:52 AM
  5. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 02:18 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
  •