java - Why does my for loop check only the first element? -
i have method checks username , password of user before login. loop checks first item, finds first condition, u.getrole().equalsignorecase("recruiter")
not satisfied first item, instead of going , checking second item breaks , returns null.
why happen?
here method:
public user check(string username, string password) throws adexception { try { begin(); query q = getsession().createquery("from user"); arraylist<user> list = (arraylist<user>) q.list(); system.out.println("recruiterlist is: " + list); (user u: list) { system.out.println("before if user is: " + u); if (u.getrole().equalsignorecase("recruiter")) { system.out.println("username 1 :" + u.getusername()); if (u.getusername().equalsignorecase(username) && u.getpassword().equalsignorecase(password)) system.out.println("after if recruiter is: " + u); system.out.println("username 2 :" + u.getusername()); return u; } break; } } catch (hibernateexception e) { rollback(); throw new adexception("unfound " + username, e); } return null; }
for (user u: list) { if (u.getrole().equalsignorecase("recruiter")) { //sysout if (u.getusername().equalsignorecase(username) && u.getpassword().equalsignorecase(password)) //2 sysout return u; } break; //here break statement exit loop after first iteration. }
so try code.
(user u: list) { if (u.getrole().equalsignorecase("recruiter")) { //sysout if (u.getusername().equalsignorecase(username) && u.getpassword().equalsignorecase(password)){ //2 sysout return u; } else{ //your code if password doesnot matched } // continue if more 1 recruiter type user object in database. }else{ //your code if user role doesnot matched } } //loop check element present in array. , if it's role matched "recuriter" check user , password.
Comments
Post a Comment