Hello everybody,
I have been working on this code segment for about three days and cannot figure out what is causing this error. I am trying to get a specific value out of an arraylist of sorted objects.
Here is the code:

public RateEstimator getMinRate()
    {
        
        
        // return null if array list empty
        
        // first sort the collection via a method call

        // now return the first element in list
        
        // declare a RateEstimator called tmp
        RateEstimator tmp;
        // check for empty array
        if (rates.size() == 0) return null;
        {
            // sort by method call
            sortByRate(); 
            //assign the value in rates position 0 to temp
            tmp = rates.get(0);
            // assign the value of the getRateMethod of temp to
            // a double called smallest
            double smallest = tmp.getTermRate();
            //return the value in smallest
            return smallest;
        }
      
    }

When I try to compile this I get the error : Incompatible types - found double but expected RateEstimator.
RateEstimator is the name of the arrayList I'm working with.

This is in fact part of a homework problem, but I have been through every Java book I own and cannot nail this down. I'm sure I'm missing something simple as I think my basic approch is correct. Any advice would be greatly apriciated.
Thanks
-Paul

Hello everybody,
I have been working on this code segment for about three days and cannot figure out what is causing this error. I am trying to get a specific value out of an arraylist of sorted objects.
Here is the code:

public RateEstimator getMinRate()
    {
        
        
        // return null if array list empty
        
        // first sort the collection via a method call

        // now return the first element in list
        
        // declare a RateEstimator called tmp
        RateEstimator tmp;
        // check for empty array
        if (rates.size() == 0) return null;
        {
            // sort by method call
            sortByRate(); 
            //assign the value in rates position 0 to temp
            tmp = rates.get(0);
            // assign the value of the getRateMethod of temp to
            // a double called smallest
            double smallest = tmp.getTermRate();
            //return the value in smallest
            return smallest;
        }
      
    }

When I try to compile this I get the error : Incompatible types - found double but expected RateEstimator.
RateEstimator is the name of the arrayList I'm working with.

This is in fact part of a homework problem, but I have been through every Java book I own and cannot nail this down. I'm sure I'm missing something simple as I think my basic approch is correct. Any advice would be greatly apriciated.
Thanks
-Paul

Look at lines 1, 22, and 24. Line 1 specifies that this function's return type is RateEstimator. Line 22 specifies that smallest has a type of double. Line 24 says:

return smallest;

Again, smallest is a double and you have declared in line 1 that you are going to return an object of type RateEstimator, not a double. That is your error. You're returning a double, not an object of type RateEstimator, in line 24. So if you want to return a double, change the function to return a double. In that case you'll have to change line 14 so it doesn't return null. If it should return an object of type RateEstimator, you'll need to change line 24.

Thanks for the help, it did indeed correct one problem.
The other problem was that I had written the Junit test wrong.
Now both problems are fixed and that part of my project is working well.
Thanks again.
-Paul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.