Yesterday I hoped I had finished program 1, but it appears to have a fault in it.
And that is that it won't take empty cells.
(It is a program that read in an excel file to a MultiHashMap, uses Apache POI)
So know I have made this:
public void OpenReadFileTwo() { // Open JFileChooser. chooser2.showOpenDialog(null); // Get the selected file. java.io.File file2 = chooser2.getSelectedFile(); // Create a FileInputStream. FileInputStream fileInput2 = null; //Get the name of the selected file. fileName2= file2.getName(); // Create a FileInputStream that will be used to read excel files. try { fileInput2 = new FileInputStream(file2); // Create an excel workbook. XSSFWorkbook workbook = new XSSFWorkbook(fileInput2); // Get the first sheet. XSSFSheet fileSheet2 = workbook.getSheetAt(0); // Create Iterator. Iterator<Row> rows2 = fileSheet2.rowIterator(); while (rows2.hasNext()) { //XSSF = .xlsx files, HSSF = .xls files. XSSFRow row = (XSSFRow) rows2.next(); // Get the empty cells in between as blank. //row.getCell(blank, Row.CREATE_NULL_AS_BLANK); //System.out.println("Blank: " + row.getCell(blank, Row.CREATE_NULL_AS_BLANK)); // Iterate the row. Iterator<Cell> cells2 = row.cellIterator(); // Count every time +1. counterFile2 = counterFile2 +1; //Get only the first column in the excel file, column A. columnNumbers2 = "A" +counterFile2; CellReference cellReference2 = new CellReference(columnNumbers2); Cell cellDataFirstColumn2 = row.getCell(cellReference2.getCol()); // Change cellDataFirstColumn2 to a String format. cellDataFirstColumn2String = "" + cellDataFirstColumn2; // Store the first column data in an arrayList. ArrayListFileData2.add(cellDataFirstColumn2String); while (cells2.hasNext()) { // XSSFCell cell2 = (XSSFCell) cells2.next(); // row.getCell(blank, Row.CREATE_NULL_AS_BLANK); //System.out.println("Blank?: " + row.getCell(blank, Row.CREATE_NULL_AS_BLANK)); String BlankOrNot = ("" + row.getCell(blank, Row.CREATE_NULL_AS_BLANK));; // if (BlankOrNot.length() != 0) { System.out.println("Fish"); //continue; Object cell2Object = "" + cell2; // mapFile2.put(cellDataFirstColumn2String, cell2Object); System.out.println("cell2Object: " + cell2Object); } else { System.out.println("Salmon"); // Object blankeCell = " "; mapFile2.put(cellDataFirstColumn2String, blankeCell); System.out.println("cell2Object: " + blankeCell); } blank = blank +1; System.out.println(blank); } blank = 0; } counterFile2++; } catch (IOException e2) { JOptionPane.showMessageDialog(null, "No file 2 selected, or the file is not a .xlsx file!"); } finally { // Show the user the name of the selected file that was loaded in. label2.setText(" File selected: " + fileName2); label2.setForeground(Color.BLUE); } }
And it looks like it is working, because it now grabs the empty cell and prints " " on my screen.
But after an empty cell have been found he runs to the catch.
And I looked up this error, and it says something is in use that I am not allowed to change.
But I don't see what is in use....
Also it only runs to the catch when an empty cell is found.
So if goes well, but else not.
And I don't understand why. -_-