0

I have a program where i am calling a method which reads xls file and fetches 2nd column.

public static String readExcel(String FileName) throws BiffException,
            IOException, java.text.ParseException, InterruptedException {

    String ss = null;
    FileInputStream fs = new FileInputStream(FileName);

    HSSFWorkbook workbook = null;

    workbook = new HSSFWorkbook(fs);

    // TO get the access to the sheet
    HSSFSheet sh = workbook.getSheet("data");

    int rowCount = sh.getLastRowNum() - sh.getFirstRowNum();
    DataFormatter formatter = new DataFormatter(Locale.US);
    for (int i = 0; i < rowCount; i++) {

        Row row = sh.getRow(i);

        // Create a loop to print cell values in a row

        for (int j = 1; j < 2; j++) {
            Cell alpha = row.getCell(j);
            String Beta = formatter.formatCellValue(alpha);
            String leftRemoved = Beta.substring(0);
            String GammaRemove = leftRemoved.trim();
            dateStringList.add(GammaRemove);
            // System.out.println(dateStringList);

        }

    }
    System.out.println("The String contains" + dateStringList);
    ArrayList<String> Beta = dateStringList;
    String[] s1 = new String[256];
    for (int k = 1; k < dateStringList.size(); k++) {
        String gamma = Beta.get(k);
        StringBuilder sb = new StringBuilder(gamma);
        sb.deleteCharAt(0);
        System.out.println("The New String is" + sb);
        // String ss1=sb.toString();
        // System.out.println("The String SS is" + ss);
        ss = sb.toString();
        System.out.println("The String SS is" + ss);
    }
    fs.close();
    return ss;

}

I am trying to use stringBuilder to remove the leading space of the 2nd column string values. But i want to fetch all these stringbuilder values from this method and store them in string array. which i am not able to do as it is throwing type mismatch error.

Please suggest how i can get all the string values from this StringBuilder and store it in string array.

Thanks

Stefan Lindner
  • 321
  • 1
  • 10
  • 20
  • 1
    If you just want to remove leading/trailing spaces, you can call the `.trim()` method on a string – Gikkman Jan 16 '17 at 13:18
  • Where in your code you are getting the error? If you only what to delete the first character in a String, you can use substring method (s.substring, 1); – pringi Jan 16 '17 at 13:22

1 Answers1

4

You don't need to use StringBuilder for it. You can directly use trim() function with your string.

String gamma = Beta.get(k);
gamma=gamma.trim();

And you can then directly save them in an Array.

Ruchika Sharma
  • 648
  • 1
  • 7
  • 23