0

I am trying to search through an arraylist and return an object. I have a Vehicle class in which I have a method named search and as parameter the registration number and arraylist are passed as parameters.

However the problem is that when going through the for each the system is never trying to compare the 2 Strings (reg numbers) and therefore the method is always returning an empty object.

Method:

 public Vehicle search(String id, List<Vehicle> myList) {
    Vehicle currenctVeh = new Vehicle();
     for(Vehicle v: myList)
     {
         if(v.regNo == id)
         {
             currenctVeh = v;
         }
     }
     return currenctVeh;
}

Being Called:

Vehicle searchVeh = new Vehicle();
            String regNum = JOptionPane.showInputDialog(null, "Enter Vehicle Registration Number");
            searchVeh.search(regNum, allVehicles);
            System.out.println(searchVeh.toString());
2
  • Compare Strings with equals(), not ==. if(v.regNo == id) becomes if(v.regNo.equals(id)). Commented Apr 10, 2014 at 21:37
  • 3
    possible duplicate of How do I compare strings in Java? Commented Apr 10, 2014 at 21:37

6 Answers 6

1

Strings are compared using equals, not ==.

== will work when working with primitive types, but with objects there is a diference:

  • == Checks if the variables reference the same objects.
  • equals Checks if the objects that the variables reference are the same (depending in the concrete implementation of equals for those objects).

You need this:

if(v.regNo.equals(id))
{
    currenctVeh = v;
}

Also, you don't need to initialize currenctVeh to a new vehicle, this would be enough:

Vehicle currenctVeh = null;
Sign up to request clarification or add additional context in comments.

Comments

0

Use v.regNo.equals(id) or else the comparison is between objects of the same address, rather than objects of the same value as should occur.

1 Comment

To answer that, I would need to see the code vor Vehicle.
0

if you can guarantee that one or the other will not be null, use something like field1.equalsIgnoreCase(field2)

or use a library that you can use StringUtils.equalsIgnoreCase(field1,field2)

Comments

0
 public Vehicle search(String id, List<Vehicle> myList) {
     for(Vehicle v: myList)
     {
         if(v.regNo.equals(id))
         {
             return v;
         }
     }
     // You should decide what to return in case of vehicle no found
     return null;
     // return new Vehicle();
}

3 Comments

ok thats good I think but why am I getting this Vehicle{price=0.0, regNo=null} when displaying with toString method
What is the desired output? @user3430861
I guess you got this output because you didn't find the vehicle in the search. Are you sure you searched for the correct vehicle id ? PS: the equals method is case sensitive, so ABC is not the same as abc. If you want it not case sensitive, use equalsIgnoreCase(...) @user3430861
0

use equals() rather than ==

As for String class equals check the content of it and == checks reference

Comments

0

By comparing two strings using == in Java, you are checking if the 2 variables you are comparing refer to the same object and not if they have the same string value.

If you want to check for string content equality, you must use the method equals.

if(v.regNo.equals(id)){.....}

For example:

String s1 = new String("Test");
String s2 = new String("Test");

s1 == s2 will give a false result as an expression, while s1.equals(s2) will give true.

Notice: String constants are kept in a constant "pool" in Java, a structure that keeps the unique constants. Therefore, "Test" == "Test" expression will give true, as we have to do with the same constant in the "pool".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.