-3

I want to check when I register new user if an username is already in use, so I have a class with a Vector<Users>, where I save all my users whit their parameters. But when I use my function usernameExist(String nuovo) that compare only the first username in the list with the string I have inserted. I have also implemented Serializable interface so I can load my list of users every time , but it work properly. If I use an Iterator to read all list I have no problem, but every function that uses for-each loop doesn't work.

 //Constructor
 public UsersList()
 {
     list = new Vector<User>();
 }

 //The method i use to check if the username exist
 public boolean usernameExist(String nuovo)
 {
    for(User user : lista)
    {
        return user.matchUsername(nuovo);
    }
    return false;
 }

 //In the class User
 //Constructor of User
 public User(String nome, String cognome, String email, String  username, String password)
 {
    //Variable initialization
 }

 public boolean matchUsername(String inserted)
 {
    return username.equalsIgnoreCase(inserted);
 }

It's making me crazy :D, thank you for the help!

Wilson
  • 11,339
  • 2
  • 29
  • 33
pezza
  • 11
  • 1
  • 5
    As is nearly always the case, you can understand how this code is running by stepping through it in a debugger. Using a debugger is not an advanced skill, it's a fundamental skill that should be learned and used as soon as possible. – T.J. Crowder Jul 10 '16 at 09:10
  • Often when you are using a new package or technique it can seem that this new technique must be the cause of your problem, when often you have forgotten something much more basic like using an `if` statement. i.e. this has nothing to do with Serialization. – Peter Lawrey Jul 10 '16 at 09:12
  • @T.J.Crowder I think the OP incorrectly assumed it was something to do with Serialization but couldn't see how (because it isn't). I usually suggest the OP use a debugger but the OP has described what you could see in a debugger anyway. The debugger won't tell you how to write code. ;) – Peter Lawrey Jul 10 '16 at 09:14
  • @PeterLawrey: No, but if you run this in the debugger and step over that return statement on the first iteration and find yourself leaving the function, it's a big clue about what the problem is. :-) – T.J. Crowder Jul 10 '16 at 09:16
  • @T.J.Crowder The OP has already noted "compare only the first username in the list with the string" but you are right that should take a couple of seconds to work out in a debugger, far less time than writing this question for example. ;) – Peter Lawrey Jul 10 '16 at 09:32

2 Answers2

3

In your loop, you're always returning at the first iteration:

for (User user : lista) {
    return user.matchUsername(nuovo); //always stops here
}

What you want instead is returning only when true:

for (User user : lista) {
    if (user.matchUsername(nuovo)) {
        return true; // stops only when true
    }
}
Joffrey
  • 32,348
  • 6
  • 68
  • 100
0

The first return statement reached will go out of the method again, therefore whenever you reach the first element inside lista, it will return:

for(User user : lista)
{
   return user.matchUsername(nuovo);
}
return false;

You could fix this by using a if-condition like here:

for(User user : lista)
{
   if(user.matchUsername(nuovo))
   {
       return true;
   }
}
return false;

This means ONLY when user.matchUsername(nuovo) returns true, the return statement will be executed.

DanielB
  • 369
  • 4
  • 15