java - Read data as double array POI -


i have code reads data excel sheet using apache poi , stores array of strings. want data array of double, since have manipulate other purposes.

i tried changing string double using double.parsedouble. data read excel file becomes @8bbab2f.

i tried initialising output double array throws error.

so how data double array using apache poi?

my code follows:

import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.poi.ss.usermodel.cell;    public class xlread {  public static void main(string args[]) throws exception {     scanner in = new scanner(system.in);     string wb_name;     wb_name = in.next();      file excel = new file("/users/documents/sample_data/" + wb_name);     fileinputstream fis = new fileinputstream(excel);       hssfworkbook wb = new hssfworkbook(fis);     hssfsheet sh = wb.getsheet("sample");      int rownum = sh.getlastrownum() + 1;     //system.out.println(rownum);     int colnum = sh.getrow(0).getlastcellnum();     //system.out.println(colnum);      string[][] x0 = new string[rownum][colnum];     // here tried define x0 double[][]. doesn't work.     (int = 3; < 368; i++){         hssfrow row = sh.getrow(i);     //system.out.println(row);         (int j = 4; j <= 27; j++){             hssfcell cell = row.getcell(j);             //system.out.println(cell);                  string value = celltostring(cell);             x0[i][j] = value;                            //system.out.println(value);         }     }     }  public static string celltostring(hssfcell cell){     // tried changing function public static double celltodouble(hssfcell cell)     int type;     object result = null;     type  = cell.getcelltype();     if(cell!=null){     if (type == cell.cell_type_formula)     {     switch (cell.getcachedformularesulttype()){         case cell.cell_type_numeric:             result = cell.getnumericcellvalue();              break;         case cell.cell_type_string:             result = cell.getstringcellvalue();             break;         default:             throw new runtimeexception("data type not applicable");     }     }     } } 

you should use double value getnumericcellvalue() returning directly without converting string in strange way , trying converting back, i.e.

public static double getcellvalue(hssfcell cell){      switch (cell.getcachedformularesulttype()){         case cell.cell_type_numeric:             return cell.getnumericcellvalue();              break;         case cell.cell_type_string:             return double.parse(cell.getstringcellvalue());            break;         default:             throw new runtimeexception("data type not applicable");     } } 

and put resulting value double[].

btw, check why there still string-formatted values trying read. if store values numeric values in excel sheet, having cell_type_numeric-case should suffice.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -