0

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

}
  }

4 Answers 4

1

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());         
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

You want to compare each Item's nome attribute to the input string - try to use voce.get(i).getNome() at the line you mentioned.

4 Comments

Thank u very much, and if the name doesn't exist how i could print "person not finded?". I assume that i don't know the exact numer of arraylist.
@Uruma- Put an else statement after the 'if' where you are trying to find the name.
yeah but if the name is in 3-4 place it will be print for 2 time not found until will find it. It's should print that, at the end of iteration.
@Uruma just use a flag (boolean variable) inside the loop to indicate a match and another if condition for checking it outside the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.