5 Replies - 256 Views - Last Post: 13 April 2012 - 08:08 AM Rate Topic: -----

#1 infernocy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 12

problem with output in arrayList

Posted 13 April 2012 - 07:39 AM

Hallo to everyone
I have some problem fixing some code .. Below is an the Depot class where it has an arraylist with vehicles .
in the code below i search and find all the cars based on the maker where i put as a parameter . the problem with the following code is that it returns both at the same time , the null that is based on not finding any cars by that maker and if it finds vehicles based on that maker .

thanks for all the help
GetMake is the method used in the Vehicle class to show the maker of the Vehicle and it is an abstract class
  public Vehicle find(String findMaker)
    {
        for (Vehicle a : vehicleList)
        {
            if (a.GetMake() == findMaker )

                System.out.println(a);

              
        }
          return null ;  


Is This A Good Question/Topic? 0
  • +

Replies To: problem with output in arrayList

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1054
  • View blog
  • Posts: 2,232
  • Joined: 30-January 11

Re: problem with output in arrayList

Posted 13 April 2012 - 07:44 AM

Using the == operator with Strings compares the memory location and not the actual series of characters that they represent.

To test String equality, you need to use the .equals method -

if(a.GetMake().equals(findmaker))
{
...
}

Was This Post Helpful? 2
  • +
  • -

#3 infernocy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 12

Re: problem with output in arrayList

Posted 13 April 2012 - 07:51 AM

Ok that is fixed thanks but what about not showing the null value together with the list
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1054
  • View blog
  • Posts: 2,232
  • Joined: 30-January 11

Re: problem with output in arrayList

Posted 13 April 2012 - 07:55 AM

I'm not entirely sure what you are talking about but surely instead of printing out the object when you find it, you want to instead return it?


public Vehicle find(String findMaker)
  {
      for (Vehicle a : vehicleList)
      {
          if (a.GetMake() == findMaker )
               return a;
      }
        return null ;  
}

Was This Post Helpful? 0
  • +
  • -

#5 infernocy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-April 12

Re: problem with output in arrayList

Posted 13 April 2012 - 08:06 AM

i think its better if i change the code to show only the Vehicle when found and nothing if the Vehicle is not found
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1054
  • View blog
  • Posts: 2,232
  • Joined: 30-January 11

Re: problem with output in arrayList

Posted 13 April 2012 - 08:08 AM

Precisely, so in the method the calls the .find method -

Vehicle v = find("something");

// If its not null (or if a Vehicle has been found)

if(v != null)
     System.out.println(v);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1