Results 1 to 12 of 12
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Why Wont This Work???

    Hi all, i have a programme of details of a business with various accessor methods and other methods i am currently stuck on how to search an array list to check if a client has the same address as another client i cant understand how i am going wrong please help?
    Here is my basic code:

    Java Code:
     public MyClass sameAddress(MyClass value) {
            MyClass result = null;
            for (int i = 0; i < myArrayList.size(); i++) {
                MyClass searchArray = (MyClass) myArrayList.get(i);
                if (searchArray.getAddress().equals(value.getAddress()))
                {
                    result = searchArray;
                }
            }
            return result;
    when i run this in the main method for example:
    Java Code:
    System.out.println(clientCollection.sameAddressCheck(client1));
    It just returns the info for client 1?
    The programme should look through the array list searchArray to find addresses until one matches value.getAddress() then return the address that matches client 1. Please Help!

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

    Default

    Is the client1 object identical to the item it finds in the arraylist?

  3. #3
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    client1 is of an object of type MyClass which has methods such as getaddress getName etc I have managed to search the Arraylist in another method which finds a client in the array which is similar to this and it works here it is:

    [CODE]
    public String find (String name) {
    String result = null;
    for (int i = 0; i < myArrayList.size(); i++) {

    MyClass searchArray = (MyClass) myArrayList.get(i);
    if (searchArray.getFullName().getSurname().equals(las tName)) {

    result = searchArray.getclient_id();
    }
    }
    return result;
    }
    [\CODE]

    this works perfectly and is done on i similar basis except in just returns the client id which is simply a field with an accessor method get client.
    Cant see why this works but the other doesnt?

  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

    The arraylist is a list of MyClass objects? If it is it's probably returning the found object.

  5. #5
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    Yes it is. So how do i fix the problem i want it to return the object in the arrayList of type MyClass that has the same address as the object entered into the parenthesis in the main method eg. (client1)

  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

    It is returning it. The object it finds with the same address happens to be the same item.

    Check the item you are searching for and the arraylist items manually, find the last one with a matching addresses.

  7. #7
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    How to i get it to return a matched address in the arraylist but not matching itself?

  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

    So you want to find an object that isn't the same as the input bit has the same address variable? You will have to overwrite equals and probably hashcode and add an additional test in the if clause.

  9. #9
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default

    The method should search MyClass for for any addresses that match in any of the objects in myarraylist by searching addresses in the Address class which is its own class with method getAddress()

    this sort of outlines what i want not sure how to finish it off correctly:
    Java Code:
    public MyClass sameAddress(MyClass value) {
    MyClass searchArray = (MyClass) myArrayList.get(i);
     if (searchArray.getAddress().equals(value.getAddress()))
    {
    return ..... of type MyClass?
    }

  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

    Create a toString method and print out the object it finds so you can see the object it returns.

  11. #11
    kwgivler is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Is this what you are trying to do?

    Java Code:
    public MyClass sameAddress(MyClass value) {
    MyClass searchArray = (MyClass) myArrayList.get(i);
    if (searchArray.getAddress().equals(value.getAddress()) &&
     !searchArray.equals(value))
    {
    return searchArray
    }

  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

    This is what I was thinking you were asking as well but I couldn't provide the code on my phone.

Similar Threads

  1. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 05:55 AM
  2. importing JCurses wont work!
    By TheBreadCat in forum New To Java
    Replies: 0
    Last Post: 02-16-2011, 08:46 PM
  3. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 01:39 AM
  4. Why wont import jm.util.*; work??
    By cakepizza in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 11:19 PM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 AM

Posting Permissions

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