I've created an arraylist, after i input with scanner a name, i would like to search if the name is equal to getName and after to print entire array with voce.get(i).toString().
Like search robert, it's search all arraylist and when found a getName who's equale to robert print al array.
Sorry for my bad english
public class Item {
private String nome,indirizzo,cellulare;
public Item(String nome, String indirizzo, String cellulare){
this.nome = nome;
this.indirizzo = indirizzo;
this.cellulare = cellulare;
}
public String toString(){
return this.getNome() + this.getIndirizzo() + this.getCellulare();
}
public String getNome() {
if(!this.nome.isEmpty()){
return this.nome;
}
else{
return "Sconosciuto";
}
}
public void setNome(String nome) {
this.nome = nome;
}
public String getIndirizzo() {
if(!this.indirizzo.isEmpty()){
return this.indirizzo;
}
else {
return "Sconosciuto";
}
}
public void setIndirizzo(String indirizzo) {
this.indirizzo = indirizzo;
}
public String getCellulare() {
if(!this.cellulare.isEmpty()){
return this.cellulare;
}
else {
return "Sconosciuto";
}
}
public void setCellulare(String cellulare) {
this.cellulare = cellulare;
}
}
MAIN:
import java.util.*;
public class AggPersone {
public static void main(String[] args) {
ArrayList<Item> voce = new ArrayList<Item>();
voce.add(new Item("Robert", "Via qualcosa", "123"));
voce.add(new Item("Roberto","Via qualcosina", "123"));
Scanner input = new Scanner(System.in);
System.out.println("chi cerchi?");
String chiave = input.nextLine();
for(int i = 0; i < voce.size(); i++){
if(chiave.equals(getNome){ <---- doesn't work, how to ispect getNome?
System.out.println(voce.get(i).toString());
}
}
}
}