Results 1 to 12 of 12
Thread: Why Wont This Work???
- 03-09-2011, 01:45 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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:
when i run this in the main method for example: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;
It just returns the info for client 1?Java Code:System.out.println(clientCollection.sameAddressCheck(client1));
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!
- 03-09-2011, 01:48 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is the client1 object identical to the item it finds in the arraylist?
- 03-09-2011, 01:57 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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?
- 03-09-2011, 02:01 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The arraylist is a list of MyClass objects? If it is it's probably returning the found object.
- 03-09-2011, 02:06 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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)
- 03-09-2011, 02:08 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-09-2011, 02:12 AM #7
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
How to i get it to return a matched address in the arraylist but not matching itself?
- 03-09-2011, 02:15 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-09-2011, 02:25 AM #9
Member
- Join Date
- Jan 2011
- Posts
- 57
- Rep Power
- 0
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? }
- 03-09-2011, 02:28 AM #10
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Create a toString method and print out the object it finds so you can see the object it returns.
- 03-09-2011, 02:31 AM #11
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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 }
- 03-09-2011, 02:33 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
why wont the %.2f work here?
By jjth39347 in forum New To JavaReplies: 2Last Post: 03-06-2011, 05:55 AM -
importing JCurses wont work!
By TheBreadCat in forum New To JavaReplies: 0Last Post: 02-16-2011, 08:46 PM -
class that wont work out properly.
By vampire-elf in forum New To JavaReplies: 7Last Post: 09-07-2010, 01:39 AM -
Why wont import jm.util.*; work??
By cakepizza in forum New To JavaReplies: 2Last Post: 01-01-2010, 11:19 PM -
save will work but load wont?!?!
By Sticks_ll in forum New To JavaReplies: 1Last Post: 06-12-2008, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks