3

I am checking every excel row and looking for certain value. After I found that value it should print an output about that row location. But if that value is incorrect or not found I am printing match not found. So everytime I am doing this my output is always: Match not found.

P.S. barcode is my string value parsed from another class (I debugged it value is passed correctly).

Moreover if I write only if case without else if program can find that value, but in case of incorrect value I will get nothing.

My problem is that everytime I run that if cycle no mater if my barcode string is correct or not I always got match not found.

My task is to scan excel file and find a value which I need in this case barcode and print entire row, but if barcode is incorrect I need to print that match not found that user will able to know that barcode he entered is incorrect.

try {
        FileInputStream file = new FileInputStream(new File("Turto sarašas 2016.09.30.xlsx"));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();
                //Check the cell type and format accordingly
                final DataFormatter df = new DataFormatter();
                String valueAsString = df.formatCellValue(cell);
                if (valueAsString.equals(barcode)) {
                    System.out.print("Hello" + row.getCell(0));
                    System.out.print("Hello" + row.getCell(3));
                } else if (!valueAsString.equals(barcode)) {
                    System.out.println(" Match not found");
                }
            }
        }

        file.close();
    } catch (IOException e) {
    }
5
  • mkyong.com/java/… Commented Sep 28, 2017 at 13:20
  • 1
    1. Never just skip exceptions because if there is one thrown you get the solution for a problem much faster. I'm not in the library you use but can the value be null? Maybe the value is null and you try to check if the value of a null string is the value you want. Maybe add a null check for the value before comparing the strings. Commented Sep 28, 2017 at 13:22
  • Value not null. Value is just a different string which is not listed in excel. Commented Sep 28, 2017 at 13:23
  • Don't know what the problem is, you should check what do you get from excel(what valueAsString equals to ) but your else if is pointless, it should be just else. Commented Sep 28, 2017 at 13:24
  • Add a simple e.printStackTrace() in the empty catch statement. And maybe put a counter instead of the 'Match not found' printout. And then print the counter's value after file.close(). Does the counter's value match the number of cells in your Excel file? Commented Oct 4, 2017 at 14:34

1 Answer 1

2

Code is working as expected. If possible share that input xlsx to debug.

Sign up to request clarification or add additional context in comments.

5 Comments

My input is 8k lines of: No match found. That is it no errors or smth like that. I even searched accross all the lines and couldn't see if my program found that 1 I was looking for.
Include below code to if statement and better comment out or delete else if statement. System.out.println( cell.getAddress().getRow() +" / "+ cell.getAddress().getColumn());
what getAddress stands for?
It's method which return row and column position currently you working on. poi.apache.org/apidocs/org/apache/poi/ss/util/CellAddress.html
But first of all you need to create Address object and after this you can call getColumn and getRow from that object, otherwise it wont gonna happen. Moreover if I delete else statement how I am going to tell user that match not found for barcode he enters?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.