1

My goal in this project is to be able to ask a user if they want to add a mechanic and bay number to their respective ArrayLists. My code works fine, but there is one annoying problem. When the while loop that starts all of the code over again runs, the prompt for if they would like to edit the schedule prints twice, once without allowing a user response. I realise there is a lot of code to read but I will print it all here, and the bug that I get every time. It would mean a lot if someone could take the time to tell me what I need to fix.

Code:

// Import packages and create main function
import java.util.Scanner;
import java.util.ArrayList;
public class schedule{
   public static void main(String []args){
      // Creates scanner and ArrayLists
      Scanner scanner = new Scanner(System.in);
      ArrayList<Integer> mechanics = new ArrayList<Integer>();
      ArrayList<Integer> bays = new ArrayList<Integer>();
      boolean edit = true;
      while(edit == true){
         edit  = editSchedule(scanner);
         // Runs AddorRemove or ends program based on user's previous choice
         if(edit == true){
            if(AddorRemove(scanner) == true){
               add(scanner, mechanics, bays);
            }
            else{
               remove(scanner, mechanics, bays);
            }
         }
         else{
            System.out.println("Have a nice day!");
         }
      }
   }
   
   // Asks user if they want to edit the schedule
   public static boolean editSchedule(Scanner scanner){
      String answer = "b";
      while(!(answer.equals("y")) || answer.equals("n")){
         System.out.print("Would you like to edit the schedule? (y/n): ");
         answer = scanner.nextLine();
         if(answer.equals("y") || answer.equals("n")){
            break;
         }
         else{
            System.out.println("Invalid response.");
         }
         
      }
      if(answer.equals("y")){
         return true;
      }
      else{
         return false;
      }
   }
   
   // Asks user if they want to add or remove an element
   public static boolean AddorRemove(Scanner scanner){
      String aor = "b";
      while(!(aor.equals("a")) || aor.equals("r")){
         System.out.print("Do you want to add or remove a mechanic and bay? (a/r): ");
         aor = scanner.nextLine();
         if(aor.equals("a") || aor.equals("r")){
            break;
         }
         
      }
      if(aor.equals("a")){
         return true;
      }
      else{
         return false;
      }
   }
   
   // Checks to see that elements are not present already and adds element to the mechanic and bay ArrayLists
   public static void add(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
      System.out.print("Enter the mechanic's ID number: ");
      int mechanic = scanner.nextInt();
      System.out.print("Enter the bay number: ");
      int bay = scanner.nextInt();
      boolean shouldAdd = true;
      for(int i=0; i<=mechanics.size()-1; i++){
         if(mechanic == mechanics.get(i)){
            System.out.println("Mechanic " + mechanic + " is already booked.");
            shouldAdd = false;
         }
      }
      for(int j=0; j<=bays.size()-1; j++){
         if(bay == bays.get(j)){
            System.out.println("Bay " + bay + " is already booked.");
            shouldAdd = false;
         }
      }
      if(shouldAdd == true){
         mechanics.add(mechanic);
         bays.add(bay);
         System.out.println("Mechanic " + mechanic + " booked for bay " + bay + ".");
      }
      else{
         System.out.println("Could not book mechanic " + mechanic + " in bay " + bay + ".");
      }
   }
   
   // Removes an element from the mechanic and bay ArrayLists
   public static void remove(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
      System.out.println("remove");
   }
}

That is the code so far. Don't worry about the remove function, as I haven't added anything to it yet. Mainly focus on the main function and the editSchedule function, which is where something goes wrong. I will now paste the problem I am having with the output.

Would you like to edit the schedule? (y/n): y
Do you want to add or remove a mechanic and bay? (a/r): a
Enter the mechanic's ID number: 1
Enter the bay number: 3
Mechanic 1 booked for bay 3.
Would you like to edit the schedule? (y/n): Invalid response.
Would you like to edit the schedule? (y/n):

All I want to know is what is making the code repeat the phrase "Would you like to edit the schedule? (y/n):" and then adding "Invalid response." If any of you need clarification or further explanation, please let me know. Thanks!

  • maybe [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/15244370) –  Feb 19 '21 at 23:24

1 Answers1

0

When you use scanner.nextInt() it doesn't complete the line. See this test as an illustration:

@Test
public void testScanner() {
    Scanner scanner = new Scanner("1\n2\n3\n4\n");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
    System.out.println("nextInt:  [" + scanner.nextInt() + "]");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
}

It produces:

nextLine: [1]
nextInt:  [2]
nextLine: []
nextLine: [3]

So after the add() method returns, you have a line ending that the scanner will read as a blank line. That gives you the Invalid response message.

Erik
  • 538
  • 8
  • I appreciate the help. Would you be able to tell me what to replace/write instead of the code I have now, so that it's fixed? I am still relatively new to Java and while I understand what you're saying, I am unsure how to apply that to my code to fix the error. – Evan Schuller Feb 20 '21 at 00:41
  • A simple option is `Integer.parseInt(scanner.nextLine())` instead of `scanner.nextInt()` – Erik Feb 20 '21 at 00:49
  • It worked! I'll keep it in mind for future projects. Many thanks! – Evan Schuller Feb 20 '21 at 01:25