1

I've got a 2D array and I need to convert each "line" of the array into a separate object array element that contains a String, an Int and multiple Doubles. Right now each 2D array element is stored as a String.

Here is my class object:

public Object() {
    String = "null";
    Double1 = -1.0;
    Double2 = -1.0;
    Double3 = -1.0;
    Integer = -1; 
}

Here is my method used to convert the 2D array to a class array:

 public static void objectConvert() {
        Object[] objArray = new Object[count];
        for (int i = 0; i<count; i++) {
            objArray[i] = new Object(data[i][0], Double.parseDouble(data[i][1]), Double.parseDouble(data[i][2]), Double.parseDouble(data[i][3]), Integer.parseInt(data[i][4]));
        }
        System.out.println(objArray[0]);
        System.out.println(objArray[1]);
    }

Here are the error that I get when compiling:

 javac Program.java

Program.java:42: error: constructor Object in class Object cannot be applied to given types;
                        Object[i] = new Object(data[i][0],
 Double.parseDouble(data[i][1]), Double.parseDouble(data[i][2]), Double.parseDouble(data[i][3]), Integer.parseInt(data[i][4]));
                                               ^
  required: no arguments
  found: String,double,double,double,int
  reason: actual and formal argument lists differ in length
1 error
1
  • 2
    Don't define a class named Object or any other predefined class. You will end up confusing yourself and other programmers. Commented Jul 31, 2015 at 4:19

2 Answers 2

1

Use objArray[i] in place of Object[i] while assigning inside the for loop.

There is one more error, you haven't defined a constructor which takes String,double,double,double,int and consider renaming your class from Object to something else.

Your constructor can be like this:

public Object(String a, Double b, Double c, Double d, Integer e) {
    string = a;
    double1 = b;
    double2 = b;
    double3 = d;
    integer = e; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

@user3264252 I guess you are new to java, please follow camel case while naming your variables.
I changed it to Object just to make it easier to understand as my actual variables are named confusingly to an outsider
0

Object[i] = new Object(data[i][0], Double.parseDouble(data[i][1]), Double.parseDouble(data[i][2]), Double.parseDouble(data[i][3]), Integer.parseInt(data[i][4]));

its

objArray[i]=  new Object(data[i][0], Double.parseDouble(data[i][1]), Double.parseDouble(data[i][2]), Double.parseDouble(data[i][3]), Integer.parseInt(data[i][4]));

Your class Object must have a constructor that have reads 5 parameter (String, double, double, double, Integer)

2 Comments

Ooh thank you, that fixed that mistake, but I'm still getting the other error: Program.java:42: error: constructor Object in class Object cannot be applied to given types; Object[i] = new Object(data[i][0], Double.parseDouble(data[i][1]), Double.parseDouble(data[i][2]), Double.parseDouble(data[i][3]), Integer.parseInt(data[i][4])); ^ required: no arguments found: String,double,double,double,int reason: actual and formal argument lists differ in length
possibly because of the absence of the constructor.

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.