-1

So i've got an assignment for class and i'm completly stuck. I've lurked around here all semester and have found what i've needed but this time i'm lost. I need help with a try catch statement inside of a loop. The assignment wants us to have people input their names and choose a schedule time 1, 2, 3, 4, 5, 6. with exceptions if it is outside of that range or if the time is already taken. heres what i've got so far. when ran, it'll let me put the first name in and a time, but gives me an error for line 40, which is the "if(timeSlot[i] <1 || >6){ " line.

Any help would be appreciated,Thanks

import java.util.Scanner;

public class Scheduler {

Scanner input = new Scanner(System.in);

public static void main(String[] args) throws TimeInUseException,
        InvalidTimeException {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    String name[] = new String[6];
    int timeSlot[] = new int[6];

    //as long as there is less than 6 it will keep running
    int d = 0;
    while (d < 6) {

        //try block to see if the time slot has been taken
        try {
            for (int i = 0; i < name.length; i++) {

                //ask for the name  
                System.out.println("Please type your name: ");
                name[i] = input.nextLine();

                //ask for the time
                System.out.println("Please select a time: 1, 2, "
                        + "3, 4, 5, 6");
                timeSlot[i] = input.nextInt();

                            // if the time is less than 1 or greater than 6
                // it will throw an invalid time exception
                if (timeSlot[i] < 1 || timeSlot[i] > 6) {
                    throw (new InvalidTimeException());
                }

                            // if the time slot is not equal to null(not empty)
                // it will throw a time in use exception
                if (timeSlot[i] != null) {
                    throw (new TimeInUseException());
                }

            }
        } // catch for invalid time
        catch (InvalidTimeException ite) {
            ite.getMessage();
        } //catch for time in use
        catch (TimeInUseException tiue) {
            tiue.getMessage();
        }
        d++;

    }
    for (int i = 0; i < 6; i++) {
        System.out.println(name[i] + " has an appointment at: "
                + timeSlot[i]);

    }

}
}

the exceptions per request

public class InvalidTimeException extends Exception{

//sets the String to say time is in use
public InvalidTimeException(String message) {
   super(message);
   message = "That time is invalid, please enter either 1, 2, 3, 4, 5, or 6";

   }

InvalidTimeException() {
    throw new UnsupportedOperationException("That time is invalid, please enter either 1, 2, 3, 4, 5, or 6"); //To change body of generated methods, choose Tools | Templates.
}


}

And the second one

public class TimeInUseException extends Exception {

//sets the String to say time is in use
     public TimeInUseException(String message) {
        super(message);
    //    message = "That time is already in use, please select another";
     }


TimeInUseException() {
    throw new UnsupportedOperationException("That time is already in use, "
            + "please select another"); 
}


}
michael.m
  • 1
  • 1
  • 3
  • Post the exception please. Although, just by looking at it those catches appear to be in the wrong place. – Azar Nov 05 '14 at 20:17
  • Adding the timeSlot[i] to the second half of that fixed that, now i've got another one – michael.m Nov 05 '14 at 20:23
  • Your code doesn't compile and is not well formatted, please could you submit the code with proper tabs? – Carlos Verdes Nov 05 '14 at 20:25
  • I also don't understand why you have a try/catch if you want to raise that exceptions (not to catch them)!! I don't get your problem really. – Carlos Verdes Nov 05 '14 at 20:26
  • I'm not sure what you mean by proper tabs? It's not compiling due to an error with the "if(timeSlot[i] != null)" on line 46. My professor said he wanted it so it would only accept the number if the value was null, it it was any other value it would throw the timeinuseexception – michael.m Nov 05 '14 at 20:32
  • Your problem is not what you think it is. The problem is with the if statement and it has nothing to do with your try catch statement. – Faiq Irfan Jul 29 '17 at 10:07

3 Answers3

1

Your catch blocks are in the wrong place. They should be immediately after your try block rather than inside. I don't exactly understand what you are trying to do with your for loop, so there are probably some other issues there, but this should be a start.

Daniel
  • 6,595
  • 9
  • 38
  • 70
0

Change your statement to if(timeSlot[i] < 1 || timeSlot[i] > 6).

jp-jee
  • 1,502
  • 3
  • 16
  • 21
0

What you've got in the line 40 is a syntatical error. You have to explicitly specify in the logical OR statement what kind of condition you want to enter - Java compiler won't guess here that you're trying to check whether or not a value in the timeSlot array is greater than 6. What you have to do is type:

if(timeSlot[i] < 1 || timeSlot[i] > 6){
    throw new InvalidTimeException();
}          

Other than that, your catch blocks are out of scope and cannot be in the for loop.

lesz3k
  • 148
  • 9
  • Thank you I've got that changed, now i just need it to accept the number if it's not null, line 46. – michael.m Nov 05 '14 at 20:28
  • just add a closing bracket, like `else if(timeSlot[i] != null) { throw(new TimeInUseException()); }` – lesz3k Nov 05 '14 at 20:29
  • I have added that bracket after null. It still gives me an error stating it's not compilable – michael.m Nov 05 '14 at 20:33
  • try to see it from this perspective: once you make the check in line 40, then there is no use to make another check whether the array is not null, because at that point you are sure that it's not null. you have 2 options - throw it out completely or move it in front of the 'greater/less than' check. – lesz3k Nov 05 '14 at 20:35
  • I have moved it up before the greater/less than check. I can't throw it out unless there is another way to see if the time hasn't been used yet. still giving me an error saying the expression is never null. Thanks for the help though – michael.m Nov 05 '14 at 20:39