Home > Mobile >  Java output/input in an unexpected loop, even though I didn't intentionally put it in a loop
Java output/input in an unexpected loop, even though I didn't intentionally put it in a loop

Time:10-15

I'm taking a java course, and in my function I have a print statement/input that keeps continuously displaying even though I didn't put it in loop. Below is my function and my output. Any ideas what might be causing this issue? -- Thank you!

private void dologin() {
    
    int count = employeeList.size();
    loggedIn = false;
    
    //scanner opened
    Scanner sc = new Scanner( System.in ); 
    
    System.out.println("Please enter your login name: "); 
    currentUser = sc.next(); 
    
    for (int i=0; i < count;   i) {
        if(currentUser == employeeList.get(i).getLogin()) { 
            currentID = employeeList.get(i).getID() ;
            System.out.println("Welcome! You are logged in!"); 
            loggedIn = true;
        } else
            System.out.println("Log in not found. Redirecting to the main menu."); 
    }
    
    //scanner closed
    sc.close();
}

Output:
Payroll Menu
1. Log In
2. Enter employees
3. List Employees
4. List employees
5. Terminate employees
6. Pay employees
0. Exit system
Please enter a selection number:
1
Please enter your login name:
mfk
Please enter your login name:
mfk
Please enter your login name:
mfk
Please enter your login name:
mfk
Please enter your login name:

public void doMenu()  { 
    
    //scanner opened
    Scanner sc = new Scanner( System.in ); 

    //prints menu 
    System.out.println(menu);

    //prompts selection and scans in selection
    System.out.println("Please enter a selection number: ");
    int selection = sc.nextInt();

    do { 
        switch (selection) { 
            case 0: 
                //writeFile();
                break; 
            case 1: 
                dologin(); 
                break; 
            case 2: 
                newEmployee(); 
                break; 
            case 3: 
                listEmployees();
                break; 
            case 4: 
                changeEmployee(); 
                break; 
            case 5: 
                terminateEmployee(); 
                break;
            case 6: 
                payEmployees(); 
                break; 
            default:
                System.out.println("Invalid entry, try again."); 
        }
            
    }while(selection!=0); 

    //Scanner is closed
    sc.close();

}

CodePudding user response:

do{}while() is a loop. You are looping while selection != 0. As you input 1 and assign to selection and never change it, sure it will loop infinitely.

  •  Tags:  
  • java
  • Related