Am I doing this exercise right?

0

So far I've been able to do it, but the imprimiAgenda() method is printing, but at the same time giving null pointer. And I also want to know if my logic is going well. If you can help me, I'd appreciate it! Another question, how do I get the getters and setters of arrays? For the setter I even managed to do, without any problem, since the getter was a problem, because it needed a return.

package exercicio;
public class Agenda {
     private String nome[] = new String [5];
     private int idade [] = new int [5];
     private float altura [] = new float [5];


public void armazenaAgenda (String nome, int idade, float altura) {
    for (int c=0; c<=5; c++) {
        if(this.nome[c] == null || this.nome[c].isEmpty()) {
        this.nome[c] = nome;
        this.idade[c] = idade;
        this.altura[c] = altura;
        break;
        } 

    }
}



public void imprimiAgenda () {
    for (int c=0; c<=5; c++){
        System.out.println("Nome: " + this.nome[c]);
        System.out.println("Idade: " + this.idade[c]);
        System.out.println("Altura: " + this.altura[c]);
        System.out.println("------------------");

    }
}

public void buscarIndex(int i) {
    for (int c=0; c<=5; c++ ){
        if(this.nome[c] == this.nome[i]) {
            System.out.println("Nome: " + this.nome[i]);
            System.out.println("Idade: " + this.idade[i]);
            System.out.println("Altura: " + this.altura[i]);
            break;
        }
    }
}

public void buscarNome (String nome) {
    for (int c=0; c<=5; c++){
        if (this.nome[c] == nome) {
            System.out.println(nome + " esta na posicaçao: " + c);
            break;
        }
    }
}
    
asked by anonymous 29.06.2018 / 19:26

1 answer

3

There are some errors. The main error is that you should not use == or != to compare String s. Use the equals method. to avoid having to worry about the fact that one of them is null in method equals , you can use Objects.equals(a, b) .

So, instead:

        if(this.nome[c] == this.nome[i]) {

You should use this:

        if (Objects.equals(this.nome[c], this.nome[i])) {

And also, instead:

        if (this.nome[c] == nome) {

You would use this:

        if (Objects.equals(this.nome[c], nome)) {

Your ties also have the wrong limits:

for (int c=0; c<=5; c++){

It was meant to be this:

for (int c = 0; c < 5; c++) {

There is yet another problem. You should not use for in method buscarIndex , after all the index is already given as parameter. Just do this:

public void buscarIndex(int i) {
    System.out.println("Nome: " + this.nome[i]);
    System.out.println("Idade: " + this.idade[i]);
    System.out.println("Altura: " + this.altura[i]);
}

There are other things that could be considered. In particular it would be a good idea to have a class Contato with nome , idade and altura and also getters and setters and then make Agenda have a List<Contato> , which also frees you from the restriction of have the maximum size of the fixed directory in 5 and would also greatly facilitate addition, removal, and search of contacts in the directory.

    
29.06.2018 / 20:18