0

I am implementing sequential search using Java. I am trying to search string from a string array. The query is obtained from keyboard using nextLine(). However I always get "not found" even when the string is clearly in the list.

/**
   Implementing sequential search
*/
public class SequentialSearch {
    public static boolean sequentialSearch(String[] names, String query) { //static method takes a string array and the query as arguments
        for (String x: names) //traverse the list
            if (x == query) {
                System.out.println("found");
                return true;
            } //end if
        System.out.println("not found"); //end for
        return false;
    } //end method
} //end class

class TestSequentialSearch {
    public static void main (String[] args) {
        String[] names = {"John", "Amy", "Tom", "Jay", "Olivia", "Jack", "Peter", "Emma"}; //a new name list
        Scanner in = new Scanner(System.in);
        String x;
        System.out.println(Arrays.toString(names));
        System.out.println("name to be searched: ");
        while (in.hasNextLine()) {
            x = in.nextLine();
            SequentialSearch.sequentialSearch(names, x); //search input in the list
            System.out.println("name to be searched: ");
        } //end while
    } //end main
} //end test

3 Answers3

2

Use equals() method instead of == for string comparisions. As equals check for string contents equality while == checks for objects equality. Change your code if condition:

From

    if (x == query)

to

if (x.equals(query))

Learn more about the difference between equals and == comparision for string from the related post here:

Java String.equals versus ==

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You need to use .equals() to compare Strings in Java, rather than ==. This is because in Java, a string is an object and == would compare the object reference - so would only be true if you had two variables pointing to the same String object.

.equals() on the other hand, compares the object CONTENT, so would look at the actual content of the string, and return true if they were equal.

This is the line I'm referring to:

if (x == query)
Chris
  • 5,882
  • 2
  • 32
  • 57
0

In Java, for non-primitive types, the == operator compares references, not values.

If you create a bunch of equivalent String literals, like:

String sLit1 = "test";
String sLit2 = "test";

(sLit1 == sLit2) will be true, since Java doesn't allocate new memory for each new String literal, it just points them all to the same location in memory. However, when you create a String object:

String sObj = new String("test") 

it will occupy a new location in memory. So (sLit1 == sObj) will always be false.

Which means that == yields true if and only if the two arguments refer to the same object. To compare strings, use equals method, as in sObj.equals(sLit1), in your case x.equals(query).

Steve P.
  • 14,489
  • 8
  • 42
  • 72