Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
  1. #21
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    You search by calling that method you've just written presumably.
    zoekOpNaam().

    How you do that in BlueJ I have no idea...that thing is so alien.

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

    Default

    in BlueJ i can do a rightclick on my PersonenLijst and say New PersonenLijst.

    I'm creating a new instance here so in my opinion it's logical that there aren't any students in there am i right?

  3. #23
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    Presumably, but as I say I have no idea how BlueJ works behind the scenes.

    From the code you've already posted, in your Go() method, why not make a call to your zoekOpNaam() method?

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

    Default

    I've called the zoekOpNaam() from the Go and this is working now.

    Is there a possibility to include a file so that i have test data in my ArrayList? I now have two records but can i include it in a simple separated file?

  5. #25
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    You'd need to read in the file and have a way of interpreting the data in it, to create the entries in the list, but it would mean you could create a single file of test data.

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

    Default

    I've tested my search function and came across the following:

    when i search my records it converted all the records to the latest add.

    I've made the voornaam variable static so that could be the problem but if i don't make this static then it complains about:

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

  7. #27
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    I think you probably need to post your code again.
    Sounds to me like you've muddled up some statics in there just to stop a compilation error, rather than actually fixing the underlying problem.

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

    Default

    Here is the current code:

    PersonenLijst Class:

    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();
        }
        
        // Run het programma
        public void Go()
        {
            Persoon persoon = new Student("Jaap","van blaat",25011983,"Man","BI","BSL");
            Persoon persoon1 = new Student("Peter","janssen",12031982,"Man","C","BSL");
            Persoon persoon2 = new Student("Peter","bossen",21121988,"Man","BI","BSL");
            Persoon persoon3 = new Docent("Karel","malen",12041976,"Man",54,234);
            personenlijst.add(persoon);
            personenlijst.add(persoon1);
            personenlijst.add(persoon2);
            personenlijst.add(persoon3);
            printList();
            zoekOpNaam();
            
        }
        
        // Zoek binnen de ArrayList, deze methode wordt standaard vanuit de Go aangeroepen.
        public void zoekOpNaam()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
           
                System.out.println(personenlijst.size());
                if(Persoon.getName().contains(inputValue)) {
                    System.out.println("YES");
                } else {
                    System.out.println("No records found!");
                }
        }
    }
    Persoon Class

    PHP Code:
    public class Persoon
    {
        private static 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;
        }
        
        // Print details
        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);
        }
        
        // Getter voornaam tbv search
        public static String getName()
        {
            return voornaam;
        }
        
        // Setter voornaam tbv search
        public void setName(String voornaam)
        {
            this.voornaam = voornaam;
        }
        
        // Getter details tbv search
        //public static String getDetails()
        //{
        //    return voornaam;
        //}
        
        // Setter details tbv search
        //public void setDetails(String voornaam, String achternaam, int geboorteDatum, String geslacht)
        //{
        //    this.voornaam = voornaam;
        //    this.achternaam = achternaam;
        //   this.geboorteDatum = geboorteDatum;
        //    this.geslacht = geslacht;
        //}
    }

  9. #29
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    OK.
    You shouldn't have anything static in Persoon.

    In your zoekOpNaam() method you are supposed to be lookign in the array list (personenlijst) for the name supplied by the user. You aren't doing that. You aren't referencing the list at all, simply making a call into Persoon as a class. Which is why I said earlier that that can't possibly work.

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

    Default

    i've adjusted the method and it compiles oke now but doesn't give me anything back even do there are records applying to the searchstring

    PHP Code:
    if(personenlijst.contains(inputValue)) {
                    System.out.println("YES");
                } else {
                    System.out.println("No records found!");
                }

  11. #31
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    Ah.
    My mistake.
    contains() requires a Persoon object (in this case).

    Go back to looping through the list and comparing the input with the name of each entry. My apologies on that one. God knows what I was thinking of.

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

    Default

    Jep.

    But when i change it back to Persoon.getName() it keeps giving me the static error ;-(

  13. #33
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    No.
    Waaay back to when you were looping over the list.
    Java Code:
            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");
                }
            }
    But I would use a foreach loop peronally.

    Oh, and obviously change the contains() call to a getName().equals() call.

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

    Default

    If put everything back but it returns, what looks like the memory address...

    PHP Code:
    public void zoekOpNaam2()
        {
            String inputValue = JOptionPane.showInputDialog("Voer de naam in waarop u wilt zoeken:");
            int index = 0;
            
            while (index < personenlijst.size()) {
                Persoon test = personenlijst.get(index);
                System.out.println(test);
                if (test.equals(inputValue)) {
                    System.out.println("YES");
                } else {
                    index++;
                    System.out.println("NO");
                    System.out.println(personenlijst.size());
                }
            }
        }
    Output:

    Totaal aantal personen: 4
    Student@638e8fbd
    NO
    4
    Student@adc9515
    NO
    4
    Student@17bf9b45
    NO
    4
    Docent@246adb31
    NO
    4

  15. #35
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    This:
    Java Code:
    System.out.println(test);
    results in a call to toString() on the object "test".
    Because you haven't given Persoon its own toString() method it uses the base one in Object, which prints out the class name and hashCode, which happens to be the memory address.

    So override toString() and construct the String you want to appear when it is called.

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

    Default

    Seems to work:-)
    PHP Code:
    // Getter complete name tbv search
        public String getCompleteName()
        {
            return voornaam + " " + achternaam;
        }
        
        // Nodig anders krijg ik memory adressen terug
        public String toString()
        {
            return "naam: " + getCompleteName();
        }

  17. #37
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,012
    Rep Power
    20

    Default

    Good stuff!

    So whenever you see something like you had before printed out you'll now know it's probably because the toString() method from that class hasn't been overidden.

Page 2 of 2 FirstFirst 12

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
  •