Nuno Teixeira
Nuno Teixeira

Reputation: 41

java - searching value inside a multi dimensional array

I need to search an array and find a certain value, if it exists returns true, and if it doesn't exist returns false.

The array:

private String zonaDeInternamento[][][] = {
    {
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "0012", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"}
    },
    {
        {"Livre", "0013", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"},
        {"Livre", "Livre", "Livre", "Livre"}
    }
};

So, If the search finds one of the numbers, it returns true, if it finds "Livre", returns false;

public boolean isPacienteInternado(String numeroProcesso) {
    if (isNumeroProcessoValido(numeroProcesso)) {
        for (int i = 0; i < zonaDeInternamento.length; i++) {
            for (int j = 0; j < zonaDeInternamento[i].length; j++) {
                for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                    if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                        System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                        return true;
                    } else {
                        System.out.println("O paciente com número de processo " + numeroProcesso + " não está internado!");
                        return false;
                    }
                }
            }
        }
    }
    return false;
}

It keeps returning false, never goes inside the if statement, returns the else message and false. Running debug if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) returns false when numeroProcesso is 13 or 12. What am I missing?

Upvotes: 2

Views: 312

Answers (4)

bwegs
bwegs

Reputation: 3767

You are only checking one item in the entire array and if it is not equal to numeroProcesso then you are exiting the loop. Instead you need to continue looping until either 1. You found a match or 2. You iterated over every item in the array. Instead you want something like this:

public boolean isPacienteInternado(String numeroProcesso) {
if (isNumeroProcessoValido(numeroProcesso)) {
    for (int i = 0; i < zonaDeInternamento.length; i++) {
        for (int j = 0; j < zonaDeInternamento[i].length; j++) {
            for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                    return true;
                } 
            }
        }
    }
    // let the user know that a match was not found
    System.out.println("Combinar não encontrado");
}
return false;

}

Upvotes: 1

chitraketu Pandey
chitraketu Pandey

Reputation: 153

You have given return false at the end of the block which is causing the error remove the return false at the bottom.

public boolean isPacienteInternado(String numeroProcesso) {
   bool value;
   if (isNumeroProcessoValido(numeroProcesso)) {
      for (int i = 0; i < zonaDeInternamento.length; i++) {
         for (int j = 0; j < zonaDeInternamento[i].length; j++) {
             for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                 if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                     System.out.println("O paciente com número de processo " + numeroProcesso + "                 está internado!");
                    bool = true;
                } else {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " não está internado!");
                    bool = false;
                }
            }
        }
    }
}
return bool;

}

Upvotes: 1

hiimjames
hiimjames

Reputation: 508

Your for loop makes only one itaration because you return true or false in first step. So delete the else statement.

if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
   System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
   return true;
}

Upvotes: 1

kamoor
kamoor

Reputation: 2939

public boolean isPacienteInternado(String numeroProcesso) {
if (isNumeroProcessoValido(numeroProcesso)) {
    for (int i = 0; i < zonaDeInternamento.length; i++) {
        for (int j = 0; j < zonaDeInternamento[i].length; j++) {
            for (int h = 0; h < zonaDeInternamento[i][j].length; h++) {
                if ((zonaDeInternamento[i][j][h].equals(numeroProcesso))) {
                    System.out.println("O paciente com número de processo " + numeroProcesso + " está internado!");
                    return true;
                }
            }
        }
    }
}
return false;}

Upvotes: 1

Related Questions