user3264252
user3264252

Reputation: 25

Convert 2D array into object array

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

Upvotes: 1

Views: 1406

Answers (2)

Ram Patra
Ram Patra

Reputation: 16674

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; 
}

Upvotes: 1

Dileep
Dileep

Reputation: 5440

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)

Upvotes: 0

Related Questions