0

I am trying to read an Excel file but I cant seem to get my code to work. The error it returns has to with Array out of bounds, but whatever number It doesnt work. Any clue where my code is incorrect?

Notes: Trying to read only the Price column and make sure the values are not NULL, N/A, less than 80 and greater than 130.

SpreadSheet:

SPreadsheet

boolean flag = true; 
File inputF = new File("data.xlsx")
InputStream getStream = new FileInputStream(inputF);

try{
    if(getStream.read() != -1){
    BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
    String line;

    while((line = reader.readLine()) != null){
        String[] csvs = line.split(",");
        for (String str : csvs){
            if (str != null && (str.equals("NA") || str.length() == 0)) {
                System.out.println(str);
                flag = false;
                break;
            }
        }
        if (!flag){
            break;
        }else{
            String Price = csvs[1]; //TODO replace with price index 
            price = price.trim();
            Integer priceValue = new Integer(Price);
            if (priceValue != null && (priceValue < 80 || priceValue > 130)){
                flag = true;
            }
        }
    }
    reader.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(flag);
return flag;

Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 

Expected Output: (Based on spreadsheet provided)

True 
BigO
  • 334
  • 1
  • 3
  • 16
  • 4
    An `xlsx` file is **not** a plain text file (like a `csv`), you cannot read its lines with a `BufferedReader`. You will have to use a library, like [Apache POI](https://poi.apache.org/). If you don't believe that, change the file extension of it from `.xlsx` to `.zip` and open the resulting archive. You will find different files and folders in it. – deHaar Sep 04 '20 at 06:23

1 Answers1

1

As stated in the comments, you should use a library, POI being an example, because most probably you get the error from the number of sheets in the excel.

Vladimir Stanciu
  • 1,468
  • 1
  • 7
  • 24