简体   繁体   中英

Java for loop with if statement only iterating once

I have a version of a login for an employee system i would like to make, I have a for loop which should go through the entire list of Accounts, then see if the name of an employee matches one in the list then the if statement continues, further questions asked etc... it seems to only iterate once and then stop as it will only find the first user and tell me the other accounts do not exisit, even though they do!! What am i doing wrong? Also my list contains Employees and Managers which inherit from Account, the if statement uses the getName in Account to compare if it equals to the user input. Sorry if this is ridiculously stupid/bad! thanks.

List <Account> Accounts = new LinkedList<Account>();   

Here is where i populate my Account, the main method calls this and the list() is called whihc contains the problematic loop

public void add() {
    Employee Geoff = new Employee("Geoff", "password1");
    Manager Bob = new Manager("Bob", "password2");
    Employee John = new Employee("John", "password3");

    Accounts.add(Geoff);
    Accounts.add(Bob);
    Accounts.add(John);
    list();

    }

problem:

System.out.println("Hello welcome: ");
System.out.println("Please enter your name: ");
String empName = Scan.nextLine();

for (Account a : Accounts) { 
        System.out.println(a);
        if (a.getname().equals(empName)) {

            System.out.println("\nPlease enter your passcode: ");

            String code = Scan.nextLine();

                if (a.check(code) == true) {
                    System.out.println("logged in");
                }
            }
        System.out.println("Employee does not exist!");
        login();

    }

I am doing the print statement in the for loop to see what it is findng, and unfortunalty it is only the first account

EDIT: I have included more code here, my after my initial if statement i want to check if the code the user enters is also correct.

see if the name of an employee matches one in the list then the if statement continues, further questions asked etc... it seems to only iterate once and then stop as it will only find the first user and tell me the other accounts do not exisit, even though they do!!

If it works for one employee and tells that others don't exist then your for loop does not iterate once.

The output you get is exactly what the code looks like. You get username once then try to match the same name with every employee in the list. If the names are equal you ask for password, otherwise you print out that employee doesn't exist. Everything right as it is in the code. You should add to your question the expected behaviour so I, or someone else can fix your code without guessing the purpose of your methods.

Here's one of those guesses:

System.out.println("Please enter your name: ");
String empName = Scan.nextLine();
boolean userFound = false;
for (Account a : Accounts) { 
        System.out.println(a);
        if (a.getname().equals(empName)) {
            System.out.println("\nPlease enter your passcode: ");
            String code = Scan.nextLine();
                if (a.check(code) == true) {
                    System.out.println("logged in");
                    userFound = true;
                    break;
                }
            }
    }

if(userFound) {
    login();
} else {
    System.out.println("User not found.");
}

This is a possible solution that doesn't use your Account class (since I do not know what it looks like) and instead uses a Map:

public static void main(String[] args)
{
   Scanner input = new Scanner(System.in);
   System.out.println("Hello welcome: ");
   System.out.println("Please enter your name: ");
   String empName = input.nextLine();
   boolean found = false;

   Map<String, String> accounts = new HashMap<String, String>();
   accounts.put("Geoff", "password1");
   accounts.put("Bob", "password2");
   accounts.put("John", "password3");

   Set<String> names = accounts.keySet();

   for (String a : names)
   {
      if (!a.equals(empName))
      {
         continue;
      }
      found = true;

      // three tries to login
      boolean success = false;
      for(int i = 0; i < 3; i++)
      {
         System.out.println("Please enter your passcode: ");
         String code = input.nextLine();

         if (accounts.get(a).equals(code))
         {
            System.out.println("logged in");
            success = true;
         }
         else
         {
            System.out.println("Wrong password... try again");
         }
      }
      if(!success)
      {
         System.out.println("User failed to authenticate after 3 attempts. User has been locked out!");
      }

   }
   if(!found)
   {
      System.out.println("Employee does not exist!");
   }
}

Since I do not know what the login() method does, I just simply added that into the code. This solution iterates three times in an attempt to get the correct password. If that fails, a message is displayed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM