0

So I've been working on an assignment that requires us to build a "Patient manager", that bases which patient to be treated first based on the emergency level. The main issue I'm running into is that when the user enters something apart from a number into the emergency column, it crashes the program and gives me an InputMismatchException, despite with me attempting to catch it. Any ideas are greatly appreciated!

import java.util.*;

    public class PatientManager {
        private PriorityQueue<Patient> waitingList;

        public PatientManager() {
            waitingList = new PriorityQueue<Patient>();
        }

        // Starter method
        public void start() {
            String choice;
            Scanner in = new Scanner(System.in);
            String name;
            int emergency = 0;
            int order = 0;
            Patient patient;
            System.out.println("-------------------------------");
            System.out.println("(1) New Patient.");
            System.out.println("(2) Next Patient.");
            System.out.println("(3) Waiting List.");
            System.out.println("(4) Exit.");
            System.out.println("-------------------------------");

            while (true) {
                System.out.print("* Choose an item from the menu:");
                choice = in.next();
                switch (choice) {
                case "1":
                    System.out.println("Enter patient's name:");
                    name = in.next();
                    System.out.println("Enter emergency [1 (low) to 5 (life-and-death)]:");

                    while (true) {
                        try {
                            emergency = in.nextInt();
                            if (emergency >= 1 && emergency <= 5)

                                break;
                            else {
                                System.out.println("(x) Wrong value. Try again:");
                                emergency = in.nextInt();
                            }
                        } catch (Exception e) {
                            System.out.println("(x) Wrong value. Try again:");
                            emergency = Integer.parseInt(in.nextLine());
                        }
                    }
                    order++;
                    patient = new Patient(order, name, emergency);
                    waitingList.add(patient);
                    System.out.println("Patient added to the waiting list.");
                    break;
                case "2":
                    if (waitingList.isEmpty()) {
                        System.out.println("No more patients.");
                    } else {
                        patient = waitingList.remove();
                        System.out.println(patient.getName() + " is treated.");
                    }
                    break;
                case "3":
                    if (waitingList.size() == 0) {
                        System.out.println("No patients in the list.");
                    } else {
                        System.out.println("Waiting list includes:");
                        Iterator<Patient> iterator = waitingList.iterator();
                        while (iterator.hasNext()) {
                            patient = (Patient) iterator.next();
                            System.out.println("- " + patient.toString());
                        }
                    }
                    break;
                case "4":
                    System.out.println("Program terminated. Good bye!!");
                    in.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("(x) Wrong choice.");
                    break;
                }
            }


        }
    }
jose praveen
  • 1,298
  • 2
  • 10
  • 17
Gabrielp
  • 15
  • 1
  • 5
  • Is it happening if you put invalid input in the first time the prompt comes up? – Kevin Mar 27 '20 at 14:45
  • No. The test I do is first setting a number higher than the limit, then one that's lower, then I attempt a text input(as per the assignment instructions, it shouldn't crash), and that's when I get an input mismatch exception. – Gabrielp Mar 27 '20 at 14:54
  • (Edited for better explanation: it happens every time anything apart from numbers are inputed) – Gabrielp Mar 27 '20 at 15:00
  • Hint: Simplify you're code. you are reading into emergency in three different places. There's no need and it's probably screwing you up. Just keep the first one immediately after the try – Kevin Mar 27 '20 at 15:24

2 Answers2

0

To start with, your code is much more complicated than it needs to be. Replace the whole while loop with this then test again.

while (emergency < 1 || emergency > 5) {
    try {
        emergency = in.nextInt();
        if (emergency < 1 || emergency > 5)
            System.out.println("(x) Wrong value. Try again:");
    }
    catch (Exception e) {
        System.out.println("(x) Wrong value. Try again:");
    }
}
Kevin
  • 7,162
  • 11
  • 46
  • 70
  • I tried this, but now it's an infinite loop of (x) Wrong value. Try again:. Even attempting to type while it happens leads to no result. – Gabrielp Mar 27 '20 at 15:42
  • @Gabrielp you did change your while(true) to while (emergency < 1 || emergency > 5) right? – Kevin Mar 27 '20 at 15:47
  • I sure did. It just looks like it's now in a permanent exception throwing loop. It doesn't actually let me type in a new value for emergency. – Gabrielp Mar 27 '20 at 15:48
  • @Gabrielp can you try printing out emergency so you can see what it has each time – Kevin Mar 27 '20 at 15:55
  • I keep getting 0 within the loop. Most likely from the initialized variable at the beginning of the program being 0. – Gabrielp Mar 27 '20 at 16:00
  • @Gabrielp I'm not sure what is going on. The emergency = in.nextInt(); line doesn't seem to be executing for some reason – Kevin Mar 27 '20 at 16:11
0

I managed to fix it. I cleared the result of scanner by just adding in.next() into the catch block. So it looks like this:

catch (Exception e) {
                    System.out.print("(x) Wrong value. Try again:");
                    in.next();
                    System.out.println(emergency);
                }
Gabrielp
  • 15
  • 1
  • 5