简体   繁体   中英

the method returns false when in fact it is true java

I have a method to go a randomaccesFile, when registering keep a char "A" if it is active and "B" if not, then an int with the number of bytes in the record and then register as such. what happens when a code is equal to that method give way to return true; but in the end I returns false

public  boolean seEncuentra(int pos , char[] codigo) {

    clsPersona contacto = new clsPersona();  //object contact
    try {

            // buscar registro apropiado en el archivo
            abrirArchivo();
            archivo.seek(pos);
            contacto.estado = archivo.readUTF();
            contacto.setTAMANIO(archivo.readInt());
            if("A".equals(contacto.estado))
            {
                for (int i = 0; i < 3; i++) {
                    contacto.codigo[i] = archivo.readChar();
                }
                if(Arrays.equals(codigo, contacto.codigo))
                {
                    return true;    //enter here and ends up returning false at the end
                }
                else
                {
                    pos+=contacto.TAMANIO;
                    seEncuentra(pos, codigo);
                }

            }
            else
            {
                pos+=contacto.TAMANIO;
                seEncuentra(pos, codigo);
            }
          cerrarArchivo();
        }


    catch (Exception ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
        cerrarArchivo();
        return false;
    }
    return false;
}

You recurse without returning the value recursed . Change

seEncuentra(pos, codigo);

to

return seEncuentra(pos, codigo);

in every place you recurse .

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