简体   繁体   中英

Java-How to print specific array from a list?

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

}
  }

If I understand it correctly, I think you are trying to see if the input from Scanner is found in the arraylist 'voce'.

You need to iterate through 'voce' until you see 'chiave'.

for(Item item: voce) {
    if(item.getNome().equals(chiave) {
         System.out.println("Found: " + item.getNome());         
    }
}

您想将每个Itemnome属性与输入字符串进行比较-尝试在您提到的行上使用voce.get(i).getNome()

You need to call the getNome() method from the Item object, like this:

for(int i = 0; i < voce.size(); i++){
   String nome = voce.get(i).getNome();
   if(chiave.equals(nome){
     System.out.println(nome);
   }
}

You are trying to use - getNome() method from item class without making an object of this class. Thus it is not even compiling.

Change your last loop to following -

for(int i = 0; i < voce.size(); i++){
                if(chiave.equals(voce.get(i).getNome())){ //<---- doesn't work, how to ispect getNome?
                    System.out.println(voce.get(i).toString());
                }
            }

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM