0

I am completing a final project for a Java course I am taking. The goal of this project is to create an Asset Management tool that someone could use to enter employees and their employee ID's, electronic devices assigned to them and the serial numbers, and then look up which devices an employee has by searching for their name or employee ID.

I keep gettin stuck on my Switch loop here. For the most part it seems to be working correctly. However after the first time through, it will loop back to the beginning and then print out the error message associated with the 'default' input option without giving the user a chance to type anything. Then it will loop back to the beginning and behave normally. I've tried switching around the orders of things and my mentor suggested that the behavior was happening because I'm using .nextLine() and .nextInt() instead of just next() for input, but when I switched to next() I just got errors instead. Not sure where to go from here.

Here's the code I have so far:


import java.util.*;

class Main {
  public static void main(String[] args) {

    //Creating a Scanner to take input from user
    Scanner input = new Scanner(System.in);

    //Creating a HashMap to take in employee names and ID numbers
    Map<String, Integer> employeeIndex = new HashMap<>();

    //Creating an ArrayList to keep track of employees and allow for easy user searching
    ArrayList<Employee> employees = new ArrayList<Employee>();

    System.out.println("This is an Asset Management Tracking program. This database allows you to add and manage employee-assigned equipment. \n");

    boolean loop = true;
    while (loop) {
    
      System.out.println("\nWould you like to add an employee to the system, or search for an employee record? Please enter your selection below: \n" + "\nAdd New Employee Record \n" + "\nSearch Employee Records \n" + "\nEnd Program \n");

      String answer1 = input.nextLine().toLowerCase();
    
      switch (answer1) {
        default: 
          System.out.println("\nSorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options. \n");
          break;
          
        case "add new employee record": System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
          String employeeName = input.nextLine();
          System.out.println("\nPlease enter their 6-digit employee ID: \n");
          int employeeID = input.nextInt();

          Employee employee1 = new Employee(employeeName, employeeID);
          employeeIndex.put(employeeName, employeeID);
          System.out.println("\nOkay. You have entered:\n\n" + "Name: " + employeeName + "\nID: " + employeeID);
          employees.add(employee1);
          System.out.println(employeeIndex.entrySet());
          break;

        case "search employee records":  System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
          String employeeNameToSearch = input.next();
          break;

        case "end program":
          System.out.println("\nThank you for using this Asset Management Program!");
          input.close();
          loop = false;
          break;
      }
    }
  }
}

I tried to add a screenshot, but I guess I can't since this is my first question.

The output of the above code currently operates as follows when the 'Add New Employee Record option is chosen:


This is an Asset Management Tracking program. This database allows you to add and manage employee-assigned equipment.

Would you like to add an employee to the system, or search for an employee record? Please enter your selection below:

Add New Employee Record

Search Employee Records

End Program

add new employee record

Please enter the employee's name (Last name, First name):

Last, First

Please enter their 6-digit employee ID:

123123

Okay. You have entered:

Name: Last, First ID: 123123 [Last, First=123123]

Would you like to add an employee to the system, or search for an employee record? Please enter your selection below:

Add New Employee Record

Search Employee Records

End Program

Sorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options.

Would you like to add an employee to the system, or search for an employee record? Please enter your selection below:

Add New Employee Record

Search Employee Records

End Program


You'll notice that once the user has finished typing in the employee ID, the program then prints out the information the user has typed in and the HashMap entry, goes back to the beginning of the loop, but then prints out the 'default' option as though the user has entered an incorrect input even though the user has not been given another chance to enter any input. Then it goes back to the beginning of the loop again correctly.

My apologies for the length of the post, but I thank you for your help.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
MiakaSHW
  • 1
  • 1
  • By the way, normally you put `default` as the last case of a `switch`, not as the first, so it reads like "when you exhausted all the other possibilities, do this". – Federico klez Culloca Sep 09 '22 at 18:22
  • Thank you for your comments! That post did answer my question but I didn't find it when I was searching earlier, so I appreciate you linking it for me. And I did have 'default' as the last case of the switch at first, but had moved it to try to figure out the solution, thinking that it was somehow an order-of-operations issue. I'll be sure to move it back and remember that. Thanks again! – MiakaSHW Sep 09 '22 at 18:44

0 Answers0