0

So I have this variable PV1 who stores from an Excel file the first row. But I want a variable that stores the entire row in a vector.

I use System.out.println() just to see if it takes the good column.

String PV1;
for(int col = 0;col < columns;col++) 
 {
   for(int row = 1;row < rows;row++) 
    {
      PV1 = sheet.getCell(1, row).getContents();
      System.out.println(Double.parseDouble(PV1)); 
    }  
 }

I am using jxl to access the Excel file.

Any help would be much appreciated!

Edit: This is the table and i need to store in PV1 all the rows.

Catalin
  • 31
  • 8

2 Answers2

0

This will help you

    String PV1;
    Object[] data = Object[columns]
    for(int col = 0;col < columns;col++) 
     {
       for(int row = 1;row < rows;row++) 
      {
         PV1 = sheet.getCell(1, row).getContents();
         data[col] = PV1;
         System.out.println(Double.parseDouble(PV1)); 
      }  
     }
Keaz
  • 955
  • 1
  • 11
  • 21
  • Actually `Object[columns]` should be `Object[columns * (rows - 1)]` and why would you use an `Object` array ? – Titus Mar 17 '17 at 11:14
  • Yeah you are correct. If you want store data as string then string array is fine. But I can see that you are converting string to a double so I assumed that you are going to convert your data at some point in your code. – Keaz Mar 17 '17 at 11:20
0

If I understand correctly you need a vector that should hold all the columns in the first row.

If that is the case, you can do something like this:

Vector<Double> firstRow = new Vector<>();
if(rows > 0){
    for(int col = 0;col < columns;col++){
        String pv = sheet.getCell(1, col).getContents();
        firstRow.add(Double.parseDouble(pv)); 
    } 
}

You should probably consider using a List instead of a Vector.

From your comment it seems that you want to retrieve a specific column from all the rows. You can do that like this:

int pv1ColumnIndex = 1;
List<Double> pv1Columns = new ArrayList<>();
for(int row = 1;row < rows;row++){// row = 1 to skip the header
    String pv1 = sheet.getCell(pv1ColumnIndex, row).getContents();
    pv1Columns.add(Double.parseDouble(pv1)); 
}
Titus
  • 22,031
  • 1
  • 23
  • 33
  • Not really. I need to store in a vector/list all the rows of a column. I uploaded a picture and that can clarify the situation. So in PV1 I want to have all the rows that the column PV1 contains. – Catalin Mar 17 '17 at 12:24
  • @AntonescuCatalin I've edited my answer. I think the second example will do what you need it to do. I'm not sure what are the parameters to the `getCell()` method. If it is `getCell(rowIndex, columnIndex)` you'll have to replace `pv1ColumnIndex` with `row` and vice versa. Success in continuare. – Titus Mar 17 '17 at 12:48