Shodmon
Shodmon

Reputation: 23

Technical: program not working as expected (exceptions, arrays)

I am making a program that is a scheduler. It is not working properly. I want three things:

1) Program keep asking until the array appointmentTime has no any empty (null) elements.

2) If entered time is not between 1 and 6 [including], throw appropriate error.

3) Throw another error if a time slot is already reserved.

Here is the code: for Scheduler class

    import java.util.Scanner;
    import java.lang.*;
    import java.io.*;
    import java.util.Arrays;
    
    public class Scheduler {

    /**
     * Writes a program that allows students to schedule appointments at either
     * 1, 2, 3, 4, 5, or 6 o’clock p. m.
     * 
     * @param args
     */
    public static void main(String[] args) {
        Scanner kbd = new Scanner(System.in);

        String[] appointmentTime = new String[6];

        int time;
        String name;
        boolean Continue;
        int temp;

        try {
            do {

                System.out.println("\nEnter your name");
                name = kbd.next();

                System.out.println("Which time slot would you like to chose?");
                time = kbd.nextInt();

                if (appointmentTime[time - 1] == null && time < 0 && time > 6) {
                    appointmentTime[time - 1] = name;
                    System.out.println(time + " is added to slot "
                            + appointmentTime[time]);

                } else if (appointmentTime[time - 1] != null) {
                    throw new TimeInUseException();

                } else if (time < 0 && time > 7) {
                    throw new InvalidTimeException();
                }

            } while (appointmentTime[time - 1] == null);

            // Final stage: printing
            System.out.println("Schedule:");
            for (int i = 0; i < appointmentTime.length; i++) {

                System.out.println((i + 1) + "PM: \t"
                        + appointmentTime[i].toString());

            }

        } catch (InvalidTimeException e) {
            System.out.println(e.getMessage());
            System.out.println("Enter another time");
        } catch (TimeInUseException ex) {
            System.out.println(ex.getMessage());
            System.out.println("Enter another time");
        } catch (ArrayIndexOutOfBoundsException ex1) {
            System.out.println(ex1.getMessage());
        } catch (NullPointerException ex2) {
            System.out.println(ex2.getMessage());
        }

    }

}

Output:

Enter your name
1
Which time slot would you like to chose?
1

Enter your name
2
Which time slot would you like to chose?
2

Enter your name
3
Which time slot would you like to chose?
3

Enter your name
4
Which time slot would you like to chose?
4

Enter your name
5
Which time slot would you like to chose?
5

Enter your name
6
Which time slot would you like to chose?
6

Enter your name
7
Which time slot would you like to chose?
7
6

It does not stop at 6

Upvotes: 0

Views: 58

Answers (2)

Shodmon
Shodmon

Reputation: 23

To make it work I changed conditions for loops. Check the code below:

package assignment9;

import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.Arrays;

public class Scheduler {
static Scanner kbd = new Scanner(System.in);

/**
 * Writes a program that allows students to schedule appointments at either
 * 1, 2, 3, 4, 5, or 6 o’clock p. m.
 * 
 * @param args
 */
public static void main(String[] args) {

    String[] appointmentTime = new String[7];

    int time;
    String name;
    int temp = 1;

    try {
        do {

            System.out.println("\nEnter your name");
            name = kbd.next();

            System.out.println("Which time slot would you like to chose?");
            time = kbd.nextInt();

            if (appointmentTime[time] == null && time > 0 && time < 7) {
                appointmentTime[time] = name;
                System.out.println(time + " is added to slot "
                        + appointmentTime[time]);

            } else if (appointmentTime[time] != null) {
                throw new TimeInUseException();

            } else if (time < 0 || time > 7) {
                throw new InvalidTimeException();
            }
            temp++;
        } while (temp < 7);

        // Final stage: printing
        System.out.println("Schedule:");
        for (int i = 1; i < 7; i++) {
        System.out.println((i) + "PM: \t"
        + appointmentTime[i].toString());}

    } catch (InvalidTimeException e) {
        System.out.println(e.getMessage());
        System.out.println("Enter another time");
    } catch (TimeInUseException ex) {
        System.out.println(ex.getMessage());
        System.out.println("Enter another time");
    } catch (ArrayIndexOutOfBoundsException ex1) {
        System.out.println(ex1.getMessage());
    } catch (NullPointerException ex2) {
        System.out.println(ex2.getMessage());
    }

}

}

Upvotes: 0

Darshan Mehta
Darshan Mehta

Reputation: 30829

Change your if condition to

if (appointmentTime[time - 1] == null && time > 0 && time < 7)

Upvotes: 1

Related Questions