简体   繁体   中英

check if the user's input is valid or invalid

I wrote a method to add an appointment. The user enters the inputs (day, time, doctor name, section name, patient name) and if the user enters the wrong doctor name or patient, it will display a message that the input is invalid until the user enters valid input.

I tried this code, but when I enter the wrong name it doesn't display the invalid message. I tried using contains , still the same.

public void add_appointment(ArrayList<appointment> appointments,
                            ArrayList<doctor> doctors,
                            ArrayList<section> sections,
                            ArrayList<Patient> PatientList,
                            ArrayList<String> dayArray,
                            ArrayList<String> timeArray) {
    Scanner input = new Scanner(System.in);
    System.out.println("==== Book appointment ====");
    System.out.println("* to book an appointment choose a doctor, section, time and day from below :");
    System.out.print("Doctors :  ");

    for (int r = 0; r < doctors.size(); r++) {
        System.out.print(doctors.get(r).getName() + ", ");
    }

    System.out.println("\n Sections : { Dermal , Dental } ");
    System.out.println("Times : from { 8AM to 4PM } ");
    System.out.println("Days : from { Sunday to Thursday } ");
    System.out.println(" ");

    System.out.println("please enter day :");
    String a1 = input.nextLine();
    if (!dayArray.contains(a1)) {
        System.out.println("invalid day , please enter another day : ");
        a1 = input.nextLine();
    }

    System.out.println("please enter time : ");
    String a2 = input.nextLine();
    if (!timeArray.contains(a2)) {
        System.out.println("invalid time , please enter another time : ");
        a2 = input.nextLine();
    }

    System.out.println("please enter the doctor name : ");
    String a3 = input.nextLine();
    for (int w = 0; w < doctors.size(); ++w) {
        if (doctors.contains(a3)) {
            System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
            a3 = input.nextLine();
        }
    }

    System.out.println("please enter the section : ");
    String a4 = input.nextLine();
    for (int e = 0; e < PatientList.size(); ++e) {
        if (sections.contains(a4)) {
            System.out.println("invalid section , the section doesn't exists. please enter section name : ");
            a4 = input.nextLine();
        }
    }

    System.out.println("please enter your name : ");
    String a5 = input.nextLine();
    for (int f = 0; f < PatientList.size(); ++f) {
        if (PatientList.contains(a5)) {
            System.out.println("invalid patient , the patient doesn't exists. please enter name again : ");
            a5 = input.nextLine();
        }
    }

    System.out.println("please assign number to your appointment : ");
    int a6 = input.nextInt();
    appointments.add(new appointment(a1, a2, a3, a4, a5, a6));
    System.out.println(appointments + " Successfully added. \n");
}

It must be something like this:

System.out.println("please enter the doctor name : ");
String a3 = input.nextLine();
while (!doctors.contains(a3)){
        System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
        a3 = input.nextLine();
    }
}

Iterate until the entered dr.name is in the list of doctors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM