3 Replies - 476 Views - Last Post: 21 February 2011 - 11:00 AM Rate Topic: -----

#1 kaix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 11

compareTO problem

Posted 21 February 2011 - 05:26 AM

boolean test = false;
for (int i=1; i<firstNames.size();i++){
GetPeopleAPIResponse response = API.getPeople(firstNames.get(i), 0, 10);
List<User> foundPeople = response.getPeople();
User foundPerson = foundPeople.get(0);
foundPerson.getFirstName();
}
if (firstNames.get(i).compareTo(foundPerson.getFirstName()) != 0){
test=true; 
System.out.println("User exists");
}
else {

System.out.println("User does not exist");



--------------------------

Hello,

Problem is the variable "test"is always false.
i think the problem is at the compareTO because i printed the names and everything works right, except that if.
This is my first post here. Hope its ok :)
Thank you,
Alex

Is This A Good Question/Topic? 0
  • +

Replies To: compareTO problem

#2 Raeghin  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 07-May 10

Re: compareTO problem

Posted 21 February 2011 - 06:20 AM

Try replacing compareTo with .equals(), you are comparing a String and Strings need to be compared with .equals();

What does
foundPerson.getFirstName();
do? If it gets the first name only then I think you forgot to assign it to a variable.
Was This Post Helpful? 1
  • +
  • -

#3 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: compareTO problem

Posted 21 February 2011 - 06:36 AM

As Raeghin suggested, using equals() will solve your problem.
To elaborate on this:
equals() checks if to Objects are equal according to the conditions specified in their equals-method. compareTo() is for sorting, thus giving you the information whether an object is greater than, lesser than or equal to another one regarding the defined order.
=> If you want to know whether two objects are equal, use equals(), if you want to sort them in any way, use compareTo().

Links for further information:
Object.equals()
Comparable
Was This Post Helpful? 1
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: compareTO problem

Posted 21 February 2011 - 11:00 AM

The compareTo in String from String API:

Quote

compareTo returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.


You can use compareTo, but the returned value you want is 0.

The problem in your code is a logic problem.
to search if a certain name is inside a List, you should loop the list and compare all values with the looked for String. if you find a match, return true.
you return false ONLY after you finished searching the whole list.

here are some guidelines:
boolean userFound = false;
for (int i=1; i<firstNames.size();i++){
  if(searchedName.equals(firstNames.get(i))){
     userFound = true;
     break;
  }
}
if(userFound == true){
   System.out.println("user found");
}
else {
   System.out.println("user not found");
} 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1