0

I want to add a float value to a float ArrayList through the .add() method. However, I'm getting this error...

no suitable method found for add(double)
method java.util.ArrayList.add(int,java.lang.Float) is not applicable
  (actual and formal argument lists differ in length)
method java.util.ArrayList.add(java.lang.Float) is not applicable
  (actual argument double cannot be converted to java.lang.Float by method invocation conversion)

This is my code...

class Exercise {
    public static void main(String[] args) {
        ArrayList<Float> floatList = new ArrayList<Float>();
        floatList.add(10.0);
        floatList.add(15.5);
        floatList.add(18.0);
        floatList.add(29.5);
        floatList.add(45.5);

        for(Float num : floatList){
            System.out.println("\n"+ num);
        }
    }
} 

Could someone please help me with this problem.

3 Answers 3

5

By default, java will use double for precision, if you want to supply float numbers, you have to do this:

ArrayList<Float> floatList = new ArrayList<Float>();
floatList.add(10.0f);
floatList.add(15.5f);
floatList.add(18.0f);
floatList.add(29.5f);
floatList.add(45.5f);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - This is the fundamental mistake. If the OP uses float literals rather than double literals, autoboxing will take care of turning the float values into Float instances. But there is no type conversion path from double to Float. (It would require a lossy conversion from a double to a float .... and that requires an explicit type cast.)
1

It's because ArrayList can only hold Objects not primitives. Remember, things such as int, float, boolean...etc are called primitives and they are NOT the same as objects. In order to get this to work, put your float into a Float wrapper as such:

Float myFloat = new Float(15.0f);

1 Comment

actually, java have a feature called Autobox for implicitly converting primitive types to Objects type (int to Integer, double to Double, float` to Float, etc).
-2
    import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.math.NumberUtils;

/**
 *
 * @author electric grasshoper
 */
public class qweqweqweqwe {

    public static void main(String[] args) {
        //Float floatList;
        ArrayList<Float> floatList = new ArrayList<Float>();
        floatList.add(10.0f);
        floatList.add(15.5f);
        floatList.add(18.0f);
        floatList.add(29.5f);
        floatList.add(45.5f);

        for(Float num : floatList){
            System.out.println("\n"+ num);
        }
  }
}

2 Comments

that's odd... why are you importing the classes from package java.sql. ?
-1 for code that has a lot of rubbish around it, and for not explaining what you changed or why you changed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.