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());
user3430861
  • 172
  • 1
  • 3
  • 14
  • Compare Strings with `equals()`, not `==`. `if(v.regNo == id)` becomes `if(v.regNo.equals(id))`. – AntonH Apr 10 '14 at 21:37
  • 3
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – rgettman Apr 10 '14 at 21:37

6 Answers6

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;
robertoia
  • 2,301
  • 23
  • 29
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.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
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)

Beta033
  • 1,975
  • 9
  • 35
  • 48
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();
}
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • ok thats good I think but why am I getting this Vehicle{price=0.0, regNo=null} when displaying with toString method – user3430861 Apr 10 '14 at 21:42
  • What is the desired output? @user3430861 – CMPS Apr 10 '14 at 21:43
  • 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 – CMPS Apr 10 '14 at 21:48
0

use equals() rather than ==

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

Gautam
  • 3,276
  • 4
  • 31
  • 53
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".

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54