Results 1 to 15 of 15
  1. #1
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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 ;      
       }

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

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Honore is offline Member
    Join Date
    Apr 2012
    Location
    Fort Wayne, Indiana
    Posts
    5
    Rep Power
    0

    Default 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?

  4. #4
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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

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

    Default Re: search from array list

    Is my reply written in invisible ink again?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default 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

  7. #7
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default 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 ;     
       }

  8. #8
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: search from array list

    Quote Originally Posted by sehudson View Post
    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 ;     
       }
    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.

  9. #9
    must0995 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: search from array list

    try if (a.GetMake().equals( findMaker) )

  10. #10
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: search from array list

    Quote Originally Posted by must0995 View Post
    try if (a.GetMake().equals( findMaker) )
    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.

  11. #11
    Honore is offline Member
    Join Date
    Apr 2012
    Location
    Fort Wayne, Indiana
    Posts
    5
    Rep Power
    0

    Default 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;
    }
    }

  12. #12
    Honore is offline Member
    Join Date
    Apr 2012
    Location
    Fort Wayne, Indiana
    Posts
    5
    Rep Power
    0

    Default Re: search from array list

    Quote Originally Posted by infernocy View Post
    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 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;
    }
    }

  13. #13
    Honore is offline Member
    Join Date
    Apr 2012
    Location
    Fort Wayne, Indiana
    Posts
    5
    Rep Power
    0

    Default Re: search from array list

    Quote Originally Posted by Honore View Post
    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;
    }
    }
    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.

  14. #14
    infernocy is offline Member
    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Default Re: search from array list

    Thanks Honore for the code it works
    Last edited by infernocy; 04-14-2012 at 02:24 PM.

  15. #15
    Honore is offline Member
    Join Date
    Apr 2012
    Location
    Fort Wayne, Indiana
    Posts
    5
    Rep Power
    0

    Default Re: search from array list

    Quote Originally Posted by Honore View Post
    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.
    No problem.

Similar Threads

  1. a list of documents for filtering search
    By fullHD in forum Lucene
    Replies: 0
    Last Post: 12-14-2011, 11:13 AM
  2. Get a token list with search results
    By icewall in forum Lucene
    Replies: 3
    Last Post: 11-15-2011, 09:16 AM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 05:37 AM
  4. vector list search
    By hezfast2 in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 06:48 PM
  5. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:38 PM

Posting Permissions

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