the loop works fine but the program doesn't execute further and compilers outputs this "[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2"
When a message is received it's split into words that may or may not be in the excel sheet if it is found a counter in the cell next to it is increased by 1. The initial problem was this " java.io.IOException: Cannot write data, document seems to have been closed already at events.Ready Events.on Event(ReadyEvents.java:99)" which I assumed occurred because I was trying to write to the excel sheet after closing it. the close() method was called inside the loop. So I moved the method outside of the loop and it doesn't even execute
here's my code
String m = ((MessageReceivedEvent) genericEvent).getMessage().getContentRaw();
String[] sep = m.split(" ");
if ( genericEvent instanceof MessageReceivedEvent){
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("New Microsoft Excel Worksheet.xlsx");
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(inputStream);
} catch (IOException e) {
System.out.println("IO exception");
}
Sheet sheet = workbook.getSheet("Sheet1");
int i=0;
do {
Row row = sheet.getRow(i);
Cell cell = row.getCell(y);
String value = cell.getStringCellValue();
System.out.println("Looping");
for(int p=0;p<100;p++){
if(sep[p] == null){
break;
}
System.out.println(sep[p] + "\n");
boolean check = sep[p].matches("(?is).*\\b" + value + "\\b.*");
if(check == true){
System.out.println("fOUND");
row = sheet.getRow(i);
cell = row.getCell(1);
cell.setCellValue(cell.getNumericCellValue()+1);
System.out.println("Done");
} }
i++;
} while (i<30);
System.out.println("out of loop");
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream("New Microsoft Excel Worksheet.xlsx");
} catch (FileNotFoundException e) {
System.out.println("CAUGHT");
}
try {
workbook.write(outputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
workbook.close();
} catch (IOException e) {
System.out.println("CAUGHT");
}
try {
outputStream.close();
} catch (IOException e) {
System.out.println("CAUGHT");
}
System.out.println("saved");