简体   繁体   中英

Can't compare sheetname with string value

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 :( 在此处输入图片说明

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

In Java, String s 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") ) {

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