Deeyennay
Deeyennay

Reputation: 13

Why doesn't this .equals() work?

I'm working in Eclipse (Android). In the following blocks, EmployeeInt and RestaurantInt are data types and query() opens a connection to the database and parses the results. When I print the query results, I get identical strings, but the boolean is still false. I've tried trimming the strings, but that didn't help.

public boolean verifyEmployee(String email, String password) {
    ArrayList<EmployeeInt> employeeEmailID = query("SELECT employeeID FROM employees WHERE emailAddress = \'"+email+"\'");
    ArrayList<EmployeeInt> employeePasswordID = query("SELECT employeeID FROM employees WHERE password = \'"+password+"\'");
    String stringEmployeeEmailID = employeeEmailID.toString();
    String stringEmployeePasswordID = employeePasswordID.toString();

    if(stringEmployeeEmailID.equals(stringEmployeePasswordID)) {
        return true;
    } else {
        return false;
    }
}

Executing the above gives me false, while executing the following block (virtually identical) gives me true.

public boolean verifyRestaurant(String email, String password) {
    ArrayList<RestaurantInt> restaurantEmailID = query("SELECT restaurantID FROM restaurants WHERE emailAddress = \'"+email+"\'");
    ArrayList<RestaurantInt> restaurantPasswordID = query("SELECT restaurantID FROM restaurants WHERE password = \'"+password+"\'");
    String stringRestaurantEmailID = restaurantEmailID.toString();
    String stringRestaurantPasswordID = restaurantPasswordID.toString();

    if(stringRestaurantEmailID.equals(stringRestaurantPasswordID)) {
        return true;
    } else {
        return false;
    }
}

Can anyone point out my mistake?

EDIT

I changed it to this and it worked:

public boolean verifyEmployee(String email, String password) {
    ArrayList<EmployeeInt> employeeEmailID = query("SELECT * FROM employees WHERE emailAddress = \'"+email+"\'");
    ArrayList<EmployeeInt> employeePasswordID = query("SELECT * FROM employees WHERE password = \'"+password+"\'");
    int intEmployeeEmailID = employeeEmailID.get(0).getID();
    int intEmployeePasswordID = employeePasswordID.get(0).getID();

    if(intEmployeeEmailID==intEmployeePasswordID) {
        return true;
    } else {
        return false;
    }
}

I know I could also use return (condition), but I would like to add some messages if the login fails, something like:

System.err.println("email address and password do not correspond");

I'm not making an app to publish, it's merely for an assignment. Thanks for the help!

Upvotes: 0

Views: 123

Answers (1)

Rainbolt
Rainbolt

Reputation: 3660

You are calling toString() on an ArrayList. Two different ArrayList objects will return two different toString() strings. You probably meant to get the first element of the ArrayList, and convert THAT to a string.

Example

EmployeeInt is your custom object. In my example, I assume it has some int field that can be retreived with getID().

ArrayList<EmployeeInt> idList = query("SELECT employeeID FROM employees WHERE emailAddress = \'"+email+"\'");
int ID = idList.get(0).getID();
stringEmployeeEmailID = String.valueOf(ID);

This may be easier to read than code:

  • query() returns an ArrayList
  • We extract the first element of the ArrayList - this is the part you left out
  • We get the ID of that element
  • We convert it to a String

Upvotes: 1

Related Questions