Am I doing this exercise right?

Asked

Viewed 63 times

0

Until then I am able to do it, but the method imprimiAgenda() is printing, but at the same time giving null Pointer. And also want to know if my logic is going well. If you can help me, I appreciate it! Another question, how do to make getters and arrays setters? Because the Setter even managed to do, without any problem, already the getter gave problem, because he 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;
        }
    }
}
  • 2

    A good object orientation could be you create a class, say, Pessoa. Within it, you would then have the attributes nome, idade and altura, encapsulated with getters and setters. So in class Agenda, instead of 1 array for every attribute, as it is today, you would have a single array, of Pessoa. With respect to your current logic, you will have problems in for because of his stopping condition. See if you can understand why.

  • so one thing, in this part here: if(this.name[c] == null || this.name[c].isEmpty()) is to check if the field of the string[Indice] = is empty so be filled with this.name[Indice] = name; got it ? how do I check if the String in the Indice X, is empty without using these operators == =!

  • yes, the Objects.equals. However it checks whether they are equal and not null, but I want on the contrary, I want to know if they are null, if they are null, and empty fill...

1 answer

3


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

So instead of this:

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

You should wear this:

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

And also, instead of that:

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

You would wear that:

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

Your ties are also on the wrong line:

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

That was supposed to be:

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

There is yet another problem. You should not use one for in the 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 do the Agenda have a List<Contato>, which also frees you from the restriction of having the maximum fixed agenda size at 5 and would also make it easier to add, remove and search for contacts in the calendar.

  • Perhaps, check that the Indice is not larger than the limit of the array, since the method and the array itself are part of the same class. Would already avoid arrayindexofboundexception.

  • Although I would do this off-schedule check and not internally.

  • @Articuno In fact I would make one return contatos.size() < i ? Optional.empty() : Optional.of(contato.get(i)); - but the OP is still far from being able to understand this.

  • 1

    I have no idea what that is. D

  • 1

    @Aricuno Store contacts in a list and use Optional<Contato> as a return of the method.

  • Thank you very much. I am starting and learning, very important your response to my development. In order to practice, I looked for exercises on the internet of java So, the idea was this, creates a schedule with the limit of 5, which would have getters methods and setters of name, height, age and also one that stores these three attributes, remove, search by name and search by index

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.