1

I am creating simple Android project which contains login Activity. The problem is I tried to reach the object in specific index and tried for loop, but it always return last object in sequence.

 public Result<LoggedInUser> login (String username, String password){

    try {
        // TODO: handle loggedInUser authentication
        GetData myData = new GetData();
        usersdata = myData.getUsers();
        for (User user : usersdata) {
            if (user.UserName.trim() == username && user.Password.trim() == password) {
                LoggedUser = user;
            } else LoggedUser = null;
        }

        return new Result.Success<>(LoggedUser);
    } catch (Exception e) {
        return new Result.Error(new IOException("Error logging in", e));
    }
}

Ilya Lysenko
  • 1,772
  • 15
  • 24
Omar
  • 17
  • 6

3 Answers3

3

Your code has three problems:

  1. Comparing the strings using == which compares the references and not the content of the strings
  2. Not breaking the loop when the intended result is found.
  3. Executing LoggedUser=null; in the else part of the if where you are comparing the strings. As a result of this, LoggedUser is set as null even when the intended result is found at any position other than the last position. You should do it once the loop terminates.

Do it as follows:

public Result<LoggedInUser> login(String username, String password) {
    boolean found = false;
    try {
        // TODO: handle loggedInUser authentication
        GetData myData=new GetData();
        usersdata=myData.getUsers();
        for (User user:usersdata) {
            if(user.UserName.trim().equals(username) && user.Password.trim().equals(password)){
                LoggedUser=user;
                found = true;
                break;
            } 
        }
        if(!found) {
            LoggedUser=null;
        }
        return new Result.Success<>(LoggedUser);    
    } catch (Exception e) {
        return new Result.Error(new IOException("Error logging in", e));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thanks gays the main problem was in the initialization of 'usersdata' must be inside loop because of these error the array filled by the same values each loop and i removed the else block because it breaks my code – Omar Mar 27 '20 at 15:48
0

We use .equals with strings as we compare references :

    public Result<LoggedInUser> login(String username, String password) {

    try {
        // TODO: handle loggedInUser authentication
        GetData myData=new GetData();
        usersdata=myData.getUsers();
        for (User user:usersdata) {

   if(user.UserName.trim().equals(username)&&user.Password.trim().equals(password)){
                LoggedUser=user;
            }
            else LoggedUser=null;
        }

        return new Result.Success<>(LoggedUser);
    } catch (Exception e) {
        return new Result.Error(new IOException("Error logging in", e));
    }
}
Shimaa Yasser
  • 587
  • 4
  • 12
0

Make sure that

username != null && password != null

Then use .equals() to compare the Strings (not == )

user.UserName.trim().equals(username.trim())&&user.Password.trim().equals(password.trim())

The reason why you received the last user in the sequence might have been, that both users were null, because then the if-statement would resolve to true for each iteration of the loop.

Benji
  • 549
  • 7
  • 22