0

I'm stuck at a part when I have to create a list and select something in it.

I have an executable

public class Executable{
    public static void main(String [] args){
        WeekEnd we = new WeekEnd();

        Personne pierre = new Personne("Pierrot");
        Personne anna = new Personne("anna");

        we.addPersonne(pierre);
        we.addPersonne(anna);

        System.out.println("test : "+we.findUsingEnhancedForLoop("anna"));

    }
}

I have my class Personne

public class Personne{
    private String name;

    public Personne(String name){
    this.name=name;
    }

    public String getPersonne(){
    return this.name;
    }
}

And my class WeekEnd where i try to select the name anna

import java.util.List;
import java.util.ArrayList;

public class WeekEnd{
    private ArrayList<Personne> listePersonne;

    public WeekEnd(){
      this.listePersonne = new ArrayList<>();
    }

   public Personne findUsingEnhancedForLoop(
   String name, ArrayList<Personne> listePersonne) {

      for (Personne personne : listePersonne) {
         if (personne.getPersonne().equals(name)) {
              return personne;
         }
     }
     return null;
   }

   public void addPersonne(Personne personne){
      listePersonne.add(personne);
   }
 }
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Antoine553
  • 126
  • 1
  • 1
  • 10
  • [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/5221149) – Andreas Sep 27 '18 at 20:05
  • 1
    Please, try to give your identifiers in English. How about I ask the question something like this: `public void изабериЛичност(`. – zlakad Sep 27 '18 at 20:07
  • ok wait a minute i edit – Antoine553 Sep 27 '18 at 20:08
  • [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/q/326569/5221149) – Andreas Sep 27 '18 at 20:08
  • @Andreas that was a just a conclusion with a little joke i can't stay serious too long – Antoine553 Sep 27 '18 at 20:11
  • So, you posted your code, but what is your question? Does the code compile or do you get an error when you try to compile it? If it compiles - does it run? What's wrong with it? At first sight I don't see anything wrong with your code. – Jesper Sep 27 '18 at 20:12
  • @zlakad is it ok like that ? – Antoine553 Sep 27 '18 at 20:13
  • @Jesper it dosn't compile it tell me that my method "findUsingEnhancedForLoop" cannot be applied to given type. Found:String required :String,ArrayList – Antoine553 Sep 27 '18 at 20:15
  • So in class Executable you call findUsingEnhancedForLoop with a String, yet in WeekEnd its parameters are String *and* ArrayList? –  Sep 27 '18 at 20:15
  • @Antoine553 Yep, it is O.K. with me - I won't down-vote... – zlakad Sep 27 '18 at 20:18
  • @RageAgainstTheVirtualMachine it seemed weird to me too but when I tried to remove it my code don't like it and the tutorial doesn't seems to have problem with it – Antoine553 Sep 27 '18 at 20:21
  • @RageAgainstTheVirtualMachine Wait i tried again and it compiled this time i'trying it – Antoine553 Sep 27 '18 at 20:22
  • @RageAgainstTheVirtualMachine ok it seemed to have worked this time i must have made an error somwhere else the previous time but instead of giving me the name Anna he is giving me Personne@66133adc do someone know why – Antoine553 Sep 27 '18 at 20:25
  • See [this](https://stackoverflow.com/a/29140403/10389796) for a thorough explanation. –  Sep 27 '18 at 20:31

2 Answers2

2

The WeekEnd class already has a listePersonne member. You shouldn't pass such an argument, which will just hide this member:

public Personne findUsingEnhancedForLoop(String name) {
    // Rest of the code, as you had it...

Note, by the way, that using Java 8's streams can clean up the method's implementation considerably:

return
listePersonne.stream().filter(p -> p.getPersonne().equals(name)).findFirst().orElse(null);
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Firstly you shouldn't pass ArrayList<Personne> listePersonne as a parameter in Weekend class. Instead of this you can pass only String name as a parameter.

public Personne findUsingEnhancedForLoop(String name) {

  for (Personne personne : listePersonne) {
     if (personne.getPersonne().equals(name)) {
          return personne;
     }
 }
 return null;
}

Also findUsingEnhancedForLoop method returns a Personne object. If you want to print a Personne's name in System.out.println() you have to override toString() method inPersonne class from Object class.

    @Override
    public String toString() {
        return this.name;
    }
Mustafa Çil
  • 766
  • 8
  • 15