Hi im doing a project for college and after finishing the majority of the code i hit a speedbump i need to register three types of users and one of them uses the department building nick as a basis for the password attribution i have an arraylist for each type, the password for this type of user should composed by the nick of the department and the number of registered users in that department so i wrote the code below which works which kinda works but the first password always gives me an empty field, the code i have is:

public String GivePasswordStaff(String signdept){

    int counter = 0;
    String password = "";

        Iterator<Staff> liststaff = staff.iterator();

        while (liststaff.hasNext()) {
            Staff lists = liststaff.next();

            if(signdept.equals(lists.getSigndept())){
                counter++;
                password = signdept + counter;

             }

       }
        return password;
    }

The thing is i need the first passwords of each department to be nick1 and with this code the first one is always "" and the second it gives the password i want except if i input a password for a different department in which case it always start with nick0 what can i do to solve this

Thanks in advance to all that reply

As far as I can see that code should return the department name followed by the number of staff in that department (or "" if there are no staff in that department), and it will always return exactly the same result unless you are changing the contents of staff outside this method - in which case we need to know about that code as well.

Yes but i want the password to be by the number of registrys like im going to insert a first staff user but since there isn't one registered the password should be like an example DC1 but it returning "" on the first ones because there isn't anyone registered and afterwards even if there is one registered if i register someone in another department the password will be DI0 for example when i want it to always start at 1

So is it the case that staff are only added to the staff collection when they get a password?
In that case your problem is that when there are no exisiting people in a dept the inside of your loop is never executed, so the return value is never set.
If you move the assignment to passwordto after the loop then it will always be executed, and if there are no staff the counter will still be 0, giving DC0. You may want to change the initialisation of counter to be 1, so that will be the first value.

Edited 6 Months Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.