2

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?

3
  • 2
    You need to iterate all the items before deciding if the item is present or not. Remove the else loop inside the nested for. Commented Dec 4, 2014 at 23:59
  • then only check for one condition in the if statement Commented Dec 5, 2014 at 0:10
  • I need this statement if (!((zonaDeInternamento[i][j][h].equals(numeroProcesso)))) to be returned as false, and need it to be inside the nested for Commented Dec 5, 2014 at 0:12

4 Answers 4

1
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;}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

2 Comments

I know that removing the else makes it work, but I need the else statement to print a message.
So keep your else statement only with System.out.println and remove the return statement from here.
1

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;

}

1 Comment

I need that return statement, else it gives an error when running
1

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;

}

Comments

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.