Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Searching and changing ArrayList

    Hello,

    I am making a PersonListing of all the student and teachers there are within a school.

    All the data is kept in an ArrayList, because i don't know how many records i have.

    I want to search through my ArrayList by Name. How can i do this?

    I already have some code:

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            int index = 0;
            boolean found = false;
            
            while (index < personenlijst.size()) {
                Persoon persoon = personenlijst.get(index);
                if (persoon.contains(inputValue)) {
                    found = true;
                    System.out.println("YES");
                } else {
                    index++;
                    System.out.println("NO");
                }
            }
        }
    It seems to go wrong at line if (persoon.contains(inputValue)) {

    I have an ArrayList form the type Persoon and i want to put in a String value. I think this is going wrong but how can i convert String --> Persoon?
    Last edited by sinister; 03-30-2011 at 07:13 AM. Reason: more info

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sinister View Post
    Hello,

    I am making a PersonListing of all the student and teachers there are within a school.

    All the data is kept in an ArrayList, because i don't know how many records i have.

    I want to search through my ArrayList by Name. How can i do this?

    I already have some code:

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            int index = 0;
            boolean found = false;
            
            while (index < personenlijst.size()) {
                Persoon persoon = personenlijst.get(index);
                if (persoon.contains(inputValue)) {
                    found = true;
                    System.out.println("YES");
                } else {
                    index++;
                    System.out.println("NO");
                }
            }
        }
    This only compiles if your Persoon object has a contains( ... ) boolean method; does it have one?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Isn't this a standard method within the ArrayList?

    my whole arraylist is stated below:

    PHP Code:
    import java.util.*;
    import javax.swing.*;
    import java.lang.*;
    
    public class PersonenLijst
    {
        private ArrayList<Persoon> personenlijst;
    
        /**
         * Constructor for objects of class PersonenLijst
         */ 
        public PersonenLijst()
        {
            personenlijst = new ArrayList<Persoon>();
        }
        
        // toevoegen aan arraylist
        public void add(Persoon persoon) 
        {
            personenlijst.add(persoon);
        }
        
        // verwijderen uit arraylist
        public void remove(Persoon persoon)
        {
            personenlijst.remove(persoon);
        }
        
        // Print complete lijst
        public void printList()
        {
            for (Persoon persoon : personenlijst) 
            {
                persoon.Print();
                System.out.println("====================================================");
            }
            System.out.println("Totaal aantal personen: " + personenlijst.size());
        }
        
        public static void main(String[] args) 
        {
            PersonenLijst p  = new PersonenLijst();
            p.Go();
        }
    
        public void Go()
        {
            Persoon persoon = new Student("Jan","van Janssen",25011983,"Man","BI","BSL");
            Persoon persoon2 = new Docent("ben","bennard",12041976,"Man",54,234);
            personenlijst.add(persoon);
            personenlijst.add(persoon2);
            printList();
        }
           
        public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            int index = 0;
            boolean found = false;
            
            while (index < personenlijst.size()) {
                Persoon persoon = personenlijst.get(index);
                if (persoon.contains(inputValue)) {
                    found = true;
                    System.out.println("YES");
                } else {
                    index++;
                    System.out.println("NO");
                }
            }
        }
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why not do something like
    Java Code:
    if(person.getName().equals(input))

  5. #5
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I've looked into that, but thought it was a little bit difficult because i need an extra method getName() and then i don't know where to start because my type is Persoon. Can you give me a start point?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Generally it's good practice to provide getters and setters for your class to control access to it's instance variables. Do you have access to the person class for editing? If you do, the method is very simple to create.

  7. #7
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    yes i do. I've created it.

    PHP Code:
    public class Persoon
    {
        private String voornaam;
        private String achternaam;
        private int geboorteDatum;
        private String geslacht;
    
        /**
         * Constructor for objects of class Persoon
         */
        public Persoon(String voornaam, String achternaam, int geboorteDatum, String geslacht)
        {
            this.voornaam = voornaam;
            this.achternaam = achternaam;
            this.geboorteDatum = geboorteDatum;
            this.geslacht = geslacht;
        }
        
        public void Print()
        {
            System.out.println("Voornaam " + this.voornaam);
            System.out.println("Achternaam " + this.achternaam);
            System.out.println("GeboorteDatum " + this.geboorteDatum);
            System.out.println("Geslacht " + this.geslacht);
        }
    }

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ok, so it would be simple enough to provide methods to read the private instance variables of a person.

  9. #9
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    oke,

    i've created a setter and getter like this.

    Persoon CLASS:
    PHP Code:
     public String getName()
        {
            return voornaam;
        }
        
        public void setName(String voornaam)
        {
            this.voornaam = voornaam;
        }
    PersonenLijst CLASS:

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            if(Persoon.getName().equals(inputValue)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    It comes back with the error:

    non-static method getName() cannot be referenced from a static context....
    Last edited by sinister; 03-30-2011 at 08:08 AM. Reason: more info

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Now in the search method ou can easily access the name of each person when doing comparisons.

  11. #11
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            if(Persoon.getName().equals(inputValue)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    It comes back with the error:

    non-static method getName() cannot be referenced from a static context....

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You need to loop through the array list checking the item at each index and see if it's name matches the input value.

  13. #13
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I've made a loop and made the variable voornaam static with the Persoon Class.

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            int index = 0;
            boolean found = false;
            
            while (index < personenlijst.size() && !found) {
                //Persoon persoon = personenlijst.get(index);
                if(Persoon.getName().equals(inputValue)) {
                    System.out.println("YES");
                    found = true;
                } else {
                    index++;
                    System.out.println("NO");
                }
            }
        }
    I don't see any output, i'm a little bit lost here.
    Last edited by sinister; 03-30-2011 at 10:40 AM. Reason: code tags

  14. #14
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    It looks like my ArrayList is empty again when i call the function zoekOpNaam() Therefor it is obvious i don't see anything because i don't even go into the while loop.

    When i manually put something in it looks my search is working.

    why is it empty...
    Last edited by sinister; 03-30-2011 at 10:58 AM.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    First off, why are you looping through the list anyway, since you already know ArrayList has a contains() method?

    Secondly, that loop (should it actually find the person) will never exit.

    ETA: Scratch that second one, you fixed it!
    Serves me right for not reading properly...:)

  16. #16
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I've removed the while loop and everything seems to work now.

    there are still a few methods i need to make so quickly going on ;-)

    FINAL version:

    PHP Code:
    public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
           
                System.out.println(personenlijst.size());
                //Persoon persoon = personenlijst.get(index);
                if(Persoon.getName().equals(inputValue)) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
        }

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    How does that even work?
    You're not even looking in the list anymore are you?

  18. #18
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    You're right i'm making new records (bluej) and then it is working. how can i let it look into the already existing arraylist?

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    What do you mean by "already existing"?
    The code in your second post only has you adding two people to the list, then printing it out. That's all.
    There's no call to this method you've been working on.

  20. #20
    sinister is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    I'm adding the two persons to the arraylist defined at the top of the class.

    How can i search in that list?

    what i do now is making a new instance through BLUEJ and add students to it. In this list i can search but how can i search in de ArrayList defined at the top?

    sorry for all the n**b answers

Page 1 of 2 12 LastLast

Similar Threads

  1. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 11:48 AM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Searching an arraylist
    By adelgado0723 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 01:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •