1

My code is supposed to perform a VERY BASIC search and print an ArrayList of books whose titles match those of a String parameter.

It works perfectly if I hardcode the parameter but for some reason it doesn't work if I save the use user input as a variable first... why does this change the output?

package com.formation;

public class Livre {

private int id;
private String title;
private int year;
private int edition;

public Livre(int id, String title, int year, int edition) {
super();
this.id = id;
this.title = title;
this.year = year;
this.edition = edition;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getEdition() {
return edition;
}

public void setEdition(int edition) {
this.edition = edition;
}

@Override
public String toString() {
return "Livre [id=" + id + ", title=" + title + ", year=" + year + ", edition=" + edition + "]";
}

}

**

package com.formation;

import java.util.ArrayList;
import java.util.List;

public class Google {

private List<Livre> Livres;

public Google(List<Livre> Bookbook) {
super();
this.Livres = Bookbook;
}

public List<Livre> search(String mot) {
List<Livre> searchResult = new ArrayList<Livre>();
for (Livre livre : this.Livres) {
    if (livre.getTitle() == mot) {
    searchResult.add(livre);
    }
}
return searchResult;
}
}

*

package com.formation;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Client {

public static void main(String[] args) {
List<Livre> books = new ArrayList<Livre>();

Livre livre1 = new Livre(1234, "TOTO", 1982, 4);

books.add(livre1);

Livre livre2 = new Livre(2345, "TATA", 1992, 2);

books.add(livre2);

Livre livre3 = new Livre(3456, "TUTU", 1962, 7);

books.add(livre3);

Livre livre4 = new Livre(4567, "TITI", 1972, 5);

books.add(livre4);

Livre livre5 = new Livre(5678, "TETE", 1952, 8);

books.add(livre5);

Google google = new Google(books);

Scanner sc = new Scanner(System.in);
System.out.println("Please enter a title:");
String book = sc.nextLine();

System.out.println(google.search(book));  //returns []
System.out.println(google.search("TATA"));  //returns [Livre [id=2345, title=TATA, year=1992, edition=2]]

}

}
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
ktouchie
  • 371
  • 1
  • 3
  • 9
  • 2
    `livre.getTitle() == mot` is the culprit, look at [How do I compare Strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). If you hardcode the parameter the same instance of the literal `"TATA"` will be used and thus it'll work while user input will result in an equal but different string instance. Use `livre.getTitle().equals(mot)`instead. – Thomas Mar 24 '16 at 10:27
  • Thanks everyone! This was an excellent way to learn about == vs .equals(). – ktouchie Mar 24 '16 at 10:41

2 Answers2

4

The reason is the == operator used for comparing Strings.

Hardcoded strings are always only defined once. So if your book title is "Test" and you hardcode "Test", then == will return true, because the hardcoded "Test" will be a reference to the very same string object.

But if you read user input first, a new String is gonna be created and the == will fail.

Try using String1.equals(String2) instead.

Mark
  • 1,498
  • 1
  • 9
  • 13
  • 1
    Yeah, ktouchie you should always use .compare() to compare strings, == compares the object reference, which is not what you want, almost evey time – Julien Mar 24 '16 at 10:31
1

You're using the == equality checking to compare the Strings. You should use the .equals method instead.

The problem is that the hardcoded Strings point to the same Object in memory, while the one you scan from the System.in gets assigned to a new Object. Though identical in content they have different memory addresses, therefore they don't match the == operator, which checks for true identity rather than logical equality.

jvalli
  • 721
  • 4
  • 7