Results 1 to 3 of 3
  1. #1
    Aaron5466 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Problem using split to put two sides of a decimal point in an array

    I am trying to write a java program using Selenium to transfer data from Excel to a website. The website won't accept numbers with decimals. The data donít have decimals in Excel, but for some reason they get decimal numbers when transferred from Excel to the java program. Iím using the getNumericCellValue from the HSSFCell class to get the data out of Excel. I couldnít figure out how not to do this, so I tried using split on ď.Ē to put the two sides of the number into two members of an array. For example, one piece of my data is 90045, which comes into my java program as 90045.0. My array is tokens[]. I am trying to put 90045 into tokens[0] and 0 into tokens[1]. But, as soon as I try accessing tokens[0], my program fails. Here is the for loop where the program fails:

    Line 198 for (int i = 0; i < xlRows; i++){
    199 HSSFRow row = xlSheet.getRow(i);
    200 for (int j = 0; j < xlCols; j++){
    201 System.out.println("just before getCell");
    202 HSSFCell cell = row.getCell(j); //To read value from each col in each row
    203 //System.out.println("i = " + i + " j = " + j);
    204 String CellData = cellToString(cell);
    205 System.out.println("Data from excel: " + CellData);
    206 tokens = CellData.split(".");
    207 System.out.println("After split");
    208 System.out.println("tokens[0]: " + tokens[0]);
    209 localArray[i][j]=tokens[0];
    210 System.out.println("After assignment to localArray ");
    211 }
    212
    213 System.out.println("");
    214 }
    Here is the method where I get the data from Excel:
    public static String cellToString(HSSFCell cell){
    //This function will convert an object of type excel cell to a string value
    int type = cell.getCellType();
    Object result;
    Object value;
    switch (type) {
    case HSSFCell.CELL_TYPE_NUMERIC: //0
    System.out.println("Cell type: Numeric");
    result = cell.getNumericCellValue();
    /*if (HSSFDateUtil.isCellDateFormatted(cell)){
    cal.setTime(HSSFDateUtil.getJavaDate(result));
    value = (String.valueOf(cal.get(Calendar.YEAR))).substring (2);
    value = (cal.get)
    }*/
    break;
    case HSSFCell.CELL_TYPE_STRING: //1
    System.out.println("Cell type: String");
    result = cell.getStringCellValue();
    break;
    case HSSFCell.CELL_TYPE_FORMULA: //2
    System.out.println("Cell type: Formula");
    throw new RuntimeException("We can't evaluate formulas in Java");
    case HSSFCell.CELL_TYPE_BLANK: //3
    System.out.println("Cell type: Blank");
    result = "-";
    break;
    case HSSFCell.CELL_TYPE_BOOLEAN: //4
    System.out.println("Cell type: Boolean");
    result = cell.getBooleanCellValue();
    break;
    case HSSFCell.CELL_TYPE_ERROR: //5
    throw new RuntimeException ("This cell has an error");
    default:
    throw new RuntimeException("We don't support this cell type: " + type);
    }
    return result.toString();
    }
    Here is my output:
    In FetchExcelData
    Before oTestDataStream creation
    After oTestDataStream creation
    Total Rows: 9
    Total Columns: 2
    just before getCell
    Cell type: Numeric
    Data from excel: 91100.0
    After split
    Here is my failure trace:
    java.lang.ArrayIndexOutOfBoundsException: 0
    at eHealthInsuranceCPackage.EHealthInsuranceC.FetchEx celData(EHealthInsuranceC.java:208)
    at eHealthInsuranceCPackage.EHealthInsuranceC.setup(E HealthInsuranceC.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.ev aluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunne r.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Problem using split to put two sides of a decimal point in an array

    Only double and float have decimals not int. Why not convert the value read from the spreadsheet into an int. Surely you know how to convert the String "12" into the number 12.

  3. #3
    Aaron5466 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Problem using split to put two sides of a decimal point in an array

    I thought I had tried that, but trying again seems to have done the trick. Thanks!

Similar Threads

  1. Method to find sum split even point in array
    By uli in forum New To Java
    Replies: 0
    Last Post: 01-31-2013, 06:57 PM
  2. Have string array point to int[] value
    By rru96 in forum New To Java
    Replies: 5
    Last Post: 09-14-2012, 10:46 AM
  3. Split BufferedImage into Array
    By Coold0wn in forum New To Java
    Replies: 3
    Last Post: 08-10-2011, 11:10 AM
  4. Need Lowest and Highest decimal point for 0.73 and 0.76
    By maran.ramar in forum New To Java
    Replies: 5
    Last Post: 11-15-2010, 01:58 AM
  5. Replies: 4
    Last Post: 01-16-2009, 06:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •