if try语句中的if语句没有检测到try / catch部分

时间:2014-11-05 20:14:51

标签: java arrays if-statement custom-exceptions

所以我上课了,我完全陷入困境。我整个学期都潜伏在这里,找到了我需要的东西,但这次我迷路了。我需要一个循环内的try catch语句的帮助。作业要求我们让人们输入他们的名字,并选择一个时间表1,2,3,4,5,6。如果超出该范围或者时间已经采用,则例外。继承人到目前为止我所拥有的。在运行时,它会让我输入第一个名字和时间,但是第40行给出了一个错误,即"if(timeSlot[i] <1 || >6){ "行。

任何帮助将不胜感激,谢谢

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]);

    }

}
}

每个请求的例外

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.
}


}

第二个

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"); 
}


}

3 个答案:

答案 0 :(得分:1)

您的catch区块位置错误。它们应该在尝试块后立即而不是中。我并不完全理解你在 for 循环中尝试做什么,所以可能还有一些其他问题,但这应该是一个开始。

答案 1 :(得分:0)

将您的陈述更改为if(timeSlot[i] < 1 || timeSlot[i] > 6)

答案 2 :(得分:0)

你在第40行得到的是一个语法错误。您必须在逻辑OR语句中明确指定要输入的条件 - Java编译器在此不会猜测您正在尝试检查timeSlot数组中的值是否大于6.您有什么要做的是输入:

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

除此之外,你的catch块超出范围,不能进入for循环。