简体   繁体   中英

How to increment my row counter variable in my while loop?

I am trying to read in each row that has data in the first cell into an ArrayList of Objects. My problem is that my code doesn't seem to be incrementing my counter past the first row. Am I missing something simple?

Code

        try
            {
                wb = new XSSFWorkbook(new FileInputStream(fileName));
            } 
        catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } 
        catch (IOException e)
            {   
                e.printStackTrace();
            }

        XSSFSheet sheet = wb.getSheetAt(2);
        ArrayList<Object> obj = new ArrayList<Object>();
        int rowIndex = 0;
        int cellIndex = 0;
        XSSFRow row = sheet.getRow(rowIndex);
        Iterator<Cell> rowItr = row.iterator();

        while(rowIndex <= sheet.getLastRowNum())
            {
                if(row.getCell(0) == null)
                    {
                        continue;
                    }
                else
                    {
                        while(rowItr.hasNext() && rowItr.next() != null)
                                    {
                                        XSSFCell cell = row.getCell(cellIndex);

                                        if(cell == null)
                                            {
                                                continue;
                                            }
                                        else
                                            {       
                                                obj.add(row.getCell(cellIndex).toString());
                                            }
                                        cellIndex++;
                                    }

                                rowIndex++;
                                cellIndex = 0;

                            }

                        System.out.println(obj.toString());
                    }
                rowIndex++;
            }   
    }

Output

[ValuSmart Series 1120 Double Hung]

... I get this output 72 times since there are 72 rows in the sheet

Isolated Loop

ArrayList<Object> obj = new ArrayList<Object>();
int rowCounter = 16;
        int x = 0;

        while(rowCounter <= 21)
            {

                XSSFRow row = sheet.getRow(rowCounter);
                Iterator<Cell> rowItr = row.iterator();

                while(rowItr.hasNext() && rowItr.next() != null)
                    {

                                XSSFCell cell = row.getCell(x);


                                if(cell == null)
                                    {
                                        continue;
                                    }
                                else
                                    {

                                        obj.add(row.getCell(x).toString());
                                    }
                        x++;

                    }

                rowCounter++;
                x = 0;
            }
        System.out.println(obj.toString());

You're not select the next row anywhere, and your loops are confusing and switch between index- and iterator-based lookups. Try a simple enhanced for loop:

for (Row row : sheet) {
    for (Cell cell : row) {
        if (cell != null) {
            obj.add(row.getCell(x).toString());
        }
    }
}
System.out.println(obj.toString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM