0

I have a method, which search in an ArrayList what contains objects with two private fields. Here is the method:

public int searchContactName(String name){

    int foundIndex =-1;
    for(int i = 0; i < contacts.size(); i++){
        if(contacts.get(i).getName() == name){
            foundIndex = i;
        }
    }

    return foundIndex;
}

It's work well if I use it like this:

searchContactName("xyName");

But if I try to use it with a scenner.next() like thins, for the same name

String name = scanner.next();
searchContactName(name);

It's give me -1. I don't understand, I need to format some way the input? It is the same type or it is different some way if I use the scanner?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    change `contacts.get(i).getName() == name` to `contacts.get(i).getName().equals(name)` – SMA May 14 '16 at 10:47
  • Have you tried using `scanner.nextLine()`? You should receive the same input only that nextLine() explicitly returns a String. – Siavas May 14 '16 at 10:47
  • How to compare Strings in Java https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html – Mad Matts May 14 '16 at 10:47

5 Answers5

0

Always use String.equals() instead of ==

Your check should be

  if(contacts.get(i).getName().equals(name))

also see this for reasons why you should not use == Java String.equals versus ==

Community
  • 1
  • 1
Vijay
  • 542
  • 4
  • 15
0

Try with

String name = scanner.nextline(); searchContactName(name);

And:

if(contacts.get(i).getName().equalsIgnpreCase ( name)) ...
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You should use equals method to compare strings values

public int searchContactName(String name){

    int foundIndex =-1;
    for(int i = 0; i < contacts.size(); i++){
        if(contacts.get(i).getName().equals(name)){
            foundIndex = i;
        }
    }

    return foundIndex;
}
V. Sambor
  • 12,361
  • 6
  • 46
  • 65
0

The == operator checks that two objects are the same instance of the object, which happens to be true for hardcoded string literals. The correct way to check this, however, is with the equals method, which tests whether two objects have the same value:

public int searchContactName(String name){

    int foundIndex =-1;
    for (int i = 0; i < contacts.size(); i++){
        if (contacts.get(i).getName().equals(name)) { // here!
            foundIndex = i;
        }
    }

    return foundIndex;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
-1

scanner.next() Will return Object in ArrayList. In your case its not exactly name. You should call getName() method on object which you got from scanner.next(). Verify code "contacts.get(i).getName()" which you wrote where its working

  • "You should call getName() method on object which you got from scanner.next()" doesn't make sense. `Scanner` is always used to scan input representing text, so `next` can only return `String` (which is technically an Object) but String class doesn't have `getName()` method. Could you clarify your answer? – Pshemo May 14 '16 at 11:00
  • Agree. My bad. Wrongly interpreted. – N Adinarayana May 14 '16 at 11:10