[英]Why doesn't this .equals() work?
I'm working in Eclipse (Android). 我正在Eclipse(Android)中工作。 In the following blocks, EmployeeInt and RestaurantInt are data types and query() opens a connection to the database and parses the results. 在以下块中,EmployeeInt和RestaurantInt是数据类型,query()打开与数据库的连接并解析结果。 When I print the query results, I get identical strings, but the boolean is still false. 当我打印查询结果时,我得到相同的字符串,但是布尔值仍然是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: 我知道我也可以使用return(条件),但是如果登录失败,我想添加一些消息,例如:
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! 谢谢您的帮助!
You are calling toString()
on an ArrayList
. 您正在ArrayList
上调用toString()
。 Two different ArrayList
objects will return two different toString()
strings. 两个不同的ArrayList
对象将返回两个不同的toString()
字符串。 You probably meant to get the first element of the ArrayList, and convert THAT to a string. 您可能打算获取ArrayList的第一个元素,并将THAT转换为字符串。
Example 例
EmployeeInt
is your custom object. EmployeeInt
是您的自定义对象。 In my example, I assume it has some int
field that can be retreived with getID()
. 在我的示例中,我假设它具有一些可以用getID()
检索的int
字段。
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
query()
返回一个ArrayList
ArrayList
- this is the part you left out 我们提取ArrayList
的第一个元素-这是您遗漏的部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.