0

What i need is to compare or search for a string in a sheet name..

I mean, the excel file might have more than one sheet, but only the sheets with a specific string let said

only sheets named "FORMATO SIVE 04" are going to be processed...

I'm debugging like crazy and I'm getting confused... my code it's like this

            Workbook eworkbook = WorkbookFactory.create(Archivo);

            int totalsheet = eworkbook.getNumberOfSheets();

            System.out.println("fuerawhile"+totalsheet);
            sheetName=new String[totalsheet];

            //String nombrehoja = eworkbook.getSheetName(0).toString();                 
            //System.out.println(nombrehoja);

            Sheet sheet = null;

            while(i<totalsheet){
                sheetName[i] = eworkbook.getSheetName(i);
                System.out.println("Antes IF :" + sheetName[i]);
                FORMATO = sheetName[i];
                if(FORMATO == "FORMATO SIVE 04"){
                    flag = true;
                }

                if(flag){
                    System.out.println("dentroIF : " + sheetName[i]);
                    sheet = eworkbook.getSheet(sheetName[i].toString());
                    System.out.println("Exito");
                }
                else if (!flag){
                    System.out.println("Error");
                }
                System.out.println("dentrowhile : " + sheetName[i]);
                i++;
            }

but it doesnt give me true :( enter image description here

does anybody has any idea?

I think this is too easy but I'm having a lot of time in this and i think I'm right :(

Thanks in advance

jompi
  • 360
  • 1
  • 4
  • 16

1 Answers1

2

In Java, Strings should never be compared using == (unless you want to test intern logic, maybe). Prefer:

if( "FORMATO SIVE 04".equals(FORMATO) ) {
....

If you are sure FORMATO will never be null, you can also use the more legible form:

if( FORMATO.equals("FORMATO SIVE 04") ) {
Mario Rossi
  • 7,651
  • 27
  • 37