Why does my for loop check only the first element?

前端 未结 5 1315
心在旅途
心在旅途 2021-01-29 07:36

I have this method that checks the username and password of a user before login. Now my for loop checks only the first item, it finds that the first condition, u.getRole()

5条回答
  •  耶瑟儿~
    2021-01-29 08:15

    Your code and logic are wrong. u doesn't change during an iteration of the for-each loop, it changes after each iteration. Your println statements suggest that you believe u is going to change during the first nested if statement. Since:

    System.out.println("userName 1 is :" + u.getUserName());
    

    and:

    System.out.println("userName 2 is :" + u.getUserName());
    

    appear in the same if block, nested in the for-each loop:

    if (u.getRole().equalsIgnoreCase("recruiter")) {
        System.out.println("userName 1 is :" + u.getUserName());
    
        if (u.getUserName().equalsIgnoreCase(userName) && u.getPassword().equalsIgnoreCase(password))
            System.out.println("After if recruiter is: " + u);
        System.out.println("userName 2 is :" + u.getUserName());
    
        return u;
    }
    

    You also don't need to use a break or continue statement. You don't need a break statement because you have a return statement. You don't need a continue statement because that's what a loop does.

    Note also that an if statement with no curly braces ({ ... }) only executes the line directly below it. For instance:

    if (u.getUserName().equalsIgnoreCase(userName) && u.getPassword().equalsIgnoreCase(password))
        System.out.println("After if recruiter is: " + u);
    

    Your code should resemble:

    public User check(String userName, String password) throws AdException {
    
        try {
            begin();
            Query q = getSession().createQuery("from User");
            ArrayList list = (ArrayList) q.list();
            System.out.println("recruiterList is: " + list);
    
            for (User u: list) {
                System.out.println("Before if user is: " + u);
    
                if (u.getRole().equalsIgnoreCase("recruiter")) {
                    System.out.println("userName 1 is :" + u.getUserName());
    
                    if (u.getUserName().equalsIgnoreCase(userName) && u.getPassword().equalsIgnoreCase(password)) {
                        System.out.println("After if recruiter is: " + u);
                        // System.out.println("userName 2 is :" + u.getUserName());
    
                        return u;
                    }
                }    
            }
    
        } catch (HibernateException e) {
            rollback();
            throw new AdException("Unfound " + userName, e);
        }
    
        return null;
    }
    

    If you want to have a println statement that outputs what the current username's index is, then don't use a for-each use a regular for loop. For instance:

    public User check(String userName, String password) throws AdException {
    
        try {
            begin();
            Query q = getSession().createQuery("from User");
            ArrayList list = (ArrayList) q.list();
            System.out.println("recruiterList is: " + list);
    
            for (int i = 0; i < list.length; i++) {
                System.out.println("Before if user is: " + u);
    
                if (u.getRole().equalsIgnoreCase("recruiter")) {
                    System.out.println("userName " + (i + 1) + " is :" + u.getUserName());
    
                    if (u.getUserName().equalsIgnoreCase(userName) && u.getPassword().equalsIgnoreCase(password)) {
                        System.out.println("After if recruiter is: " + u);
    
                        return u;
                    }
                }    
            }
    
        } catch (HibernateException e) {
            rollback();
            throw new AdException("Unfound " + userName, e);
        }
    
        return null;
    }
    

提交回复
热议问题