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;
}
}
}
A good object orientation could be you create a class, say,
Pessoa
. Within it, you would then have the attributesnome
,idade
andaltura
, encapsulated with getters and setters. So in classAgenda
, instead of 1 array for every attribute, as it is today, you would have a single array, ofPessoa
. With respect to your current logic, you will have problems infor
because of his stopping condition. See if you can understand why.– StatelessDev
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 == =!
– Daniel Sampaio
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...
– Daniel Sampaio