Results 1 to 15 of 15
Thread: search from array list
- 04-13-2012, 04:13 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
search from array list
Hallo
This is method inside a a depot class that search the vehicle class and shows the vehicles by their maker
the problem at the moment is that it shows both the null and the cars at the same time ,if a car is there -
Java Code:public Vehicle find(String findMaker) { for (Vehicle a : vehicleList) { if (a.GetMake() == findMaker ) System.out.println(a); } return null ; }
- 04-13-2012, 04:48 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: search from array list
Don't compare Strings for equality with the == operator; use the equals( ... ) method and read the API documentation for the String class to see why.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-13-2012, 04:58 PM #3
Member
- Join Date
- Apr 2012
- Location
- Fort Wayne, Indiana
- Posts
- 5
- Rep Power
- 0
Re: search from array list
What's your problem? Do you want your method to be able to return a null value or output the car maker?
- 04-13-2012, 05:36 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: search from array list
the method should return the vehicle if found , so i think its better if i change to show nothing if not found , the problem is at the moment it shows both at the same time ( the null value and the car found )
Last edited by infernocy; 04-13-2012 at 05:46 PM. Reason: change
- 04-13-2012, 05:49 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: search from array list
Is my reply written in invisible ink again?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-13-2012, 05:55 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: search from array list
JosAh i am looking at the APi documentation and i did read you reply --- i am still new in java
- 04-13-2012, 07:11 PM #7
Re: search from array list
What does your output look like?
From what you have posted, your output will be the toString() method for your Vehicle class.
The toString() method for the Object class returns a string containing the name of the class of which the object is an instance, the @ sign, and the unsigned hex representation of the hash code of the object.
You can override the toString() method and write your own, if that is what you are trying to have displayed. Maybe you just want to have the find method return a Vehicle object where the maker is equal to findMaker.
Java Code:public Vehicle find(String findMaker) { for (Vehicle a : vehicleList) { if (a.GetMake() == findMaker ) return a; } return null ; }
- 04-13-2012, 09:25 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: search from array list
i input a car maker and it should search the vehicle list and list all the Vehicles by that maker ,at the moment if there is a list of vehicles , it will return the null value as well as the toString method , i want to correct it so it will not return null if there are some cars , but i think its better if it will do nothing if there are no cars by that maker .
Last edited by infernocy; 04-13-2012 at 09:56 PM.
- 04-13-2012, 10:02 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 7
- Rep Power
- 0
Re: search from array list
try if (a.GetMake().equals( findMaker) )
- 04-13-2012, 10:23 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: search from array list
ah i forgot to mention that i already change that
it will return the null value as well as show the output of the toString method inside the vehicle class , i want to correct it so it will not return the null value if there are some cars, but i think its better if i replace the returned null value with do nothing , so it will not do anything is there are no cars by that maker in the vehicle list
public Vehicle find(String findMaker)
{
for (Vehicle a : vehicleList)
{
if (a.GetMake().equals(findMaker ))
System.out.println(a);
}
return null;
}Last edited by infernocy; 04-13-2012 at 10:32 PM.
- 04-14-2012, 08:44 AM #11
Member
- Join Date
- Apr 2012
- Location
- Fort Wayne, Indiana
- Posts
- 5
- Rep Power
- 0
Re: search from array list
public static String find(String findMaker)
{
int numVehicles = 0;
Vehicle v = null;
for (Vehicle a : vehicleList)
{
if (a.GetMake().equals(findMaker ))
{
System.out.println(a);
numVehicles++;
v = a;
}
}
if(numVehicles > 0)
{
return v;
}
else
{
return null;
}
}
- 04-14-2012, 08:46 AM #12
Member
- Join Date
- Apr 2012
- Location
- Fort Wayne, Indiana
- Posts
- 5
- Rep Power
- 0
Re: search from array list
- 04-14-2012, 08:59 AM #13
Member
- Join Date
- Apr 2012
- Location
- Fort Wayne, Indiana
- Posts
- 5
- Rep Power
- 0
Re: search from array list
The "find method" will return a null value if the warehouse does not have a vehicle by that maker. If it does, then the "find method" will output the vehicle's name to the console and also return it to the calling method. If you want the "find method" to return nothing if a vehicle is found, then you must change the return value to void.
- 04-14-2012, 10:15 AM #14
Member
- Join Date
- Apr 2012
- Posts
- 15
- Rep Power
- 0
Re: search from array list
Thanks Honore for the code it works
Last edited by infernocy; 04-14-2012 at 02:24 PM.
- 04-14-2012, 03:50 PM #15
Member
- Join Date
- Apr 2012
- Location
- Fort Wayne, Indiana
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
a list of documents for filtering search
By fullHD in forum LuceneReplies: 0Last Post: 12-14-2011, 11:13 AM -
Get a token list with search results
By icewall in forum LuceneReplies: 3Last Post: 11-15-2011, 09:16 AM -
Linked List, Array List time complexity
By Rick99771977 in forum New To JavaReplies: 4Last Post: 08-18-2011, 05:37 AM -
vector list search
By hezfast2 in forum New To JavaReplies: 2Last Post: 06-14-2008, 06:48 PM -
How to Search a List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks