首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法捕获InputMismatchException

无法捕获InputMismatchException
EN

Stack Overflow用户
提问于 2020-03-27 22:14:47
回答 2查看 140关注 0票数 0

因此,我一直在做一项任务,要求我们建立一个“患者管理器”,根据急救级别首先治疗哪个患者。我遇到的主要问题是,当用户在紧急列中输入除数字之外的其他内容时,它会使程序崩溃,并给我一个InputMismatchException,尽管我试图捕获它。任何想法都是非常感谢的!

代码语言:javascript
复制
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;
                }
            }


        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-28 00:20:49

我设法修好了它。我只需将in.next()添加到catch块中,即可清除scanner的结果。所以看起来是这样的:

代码语言:javascript
复制
catch (Exception e) {
                    System.out.print("(x) Wrong value. Try again:");
                    in.next();
                    System.out.println(emergency);
                }
票数 0
EN

Stack Overflow用户

发布于 2020-03-27 23:28:48

首先,你的代码比它需要的复杂得多。用这个替换整个while循环,然后再次测试。

代码语言:javascript
复制
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:");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60887785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档