未创建或显示异常

时间:2014-05-18 01:26:55

标签: java loops exception while-loop

我正在进行一项任务,我必须创建一个计划并处理错误输入的某些异常。我主要工作,但对于我的通用异常抛出它似乎并没有创建我想要的对象。我使用重载的构造函数来创建它,当我调用消息时它不显示我的输入。我的代码如下:

import java.util.*;
import java.lang.Exception;

public class Scheduler{
public static void main() throws Exception{
    Scanner input = new Scanner(System.in);
    String name;
    int time;

    // Program introduction

    System.out.println("Welcome to scheduling scheduler 2.0");
    System.out.println("You can schedule an appointment at 1, 2, 3, 4, 5, or 6 pm");

    // Creating the Array

    String[] schedule = new String[6];

    // While loop set to 

    while(filled(schedule) == false){
        System.out.print("What is your name? ");
        name = input.next();
        boolean innerExecute = true;

        // Do While loop used in order to loop until a valid time is set instead of asking for
        // name again

        do{
            try{
                System.out.print("When do you want to schedule your appointment? ");
                time = input.nextInt();
                System.out.println();
                if(time > 6 || time < 1){
                    throw new InvalidTimeException("Sorry that is not a valid time");
                }

                else if(schedule[time - 1] == null){
                    schedule[time - 1] = name;
                    innerExecute = false;
                }

                else if(schedule[time - 1] != null){
                    throw new TimeInUseException("That time is not available.");
                }

                else{
                    throw new Exception("Bad time format, should be an integer.");
                }

            }

            catch(TimeInUseException e){
                System.out.println(e.getMessage());
                System.out.println();
            }
            catch(InvalidTimeException e){
                System.out.println(e.getMessage());
                System.out.println();
            }
            catch(Exception e){
                System.out.println(e.getMessage());
                System.out.println();
                String voided = input.next();
            }

        }while(innerExecute == true);
    }

    System.out.println("The schedule is as follows: ");
    System.out.println();
    for(int i=0; i<6; i++){
        System.out.print("At " + (i+1) + " PM is " + schedule[i]);
        System.out.println();
    }
}

我的输出是:

Welcome to scheduling scheduler 2.0
You can schedule an appointment at 1, 2, 3, 4, 5, or 6 pm
What is your name? don
When do you want to schedule your appointment? 
,
null

When do you want to schedule your appointment? 

所有其他异常都会正确显示,但是这个异常会打印出来。

2 个答案:

答案 0 :(得分:1)

简单地说,当nextInt()发现要返回int以外的内容时,它会抛出InputMismatchException而没有消息。因此getMessage()会返回null。请尝试使用Exception#printStackTrace()来查看堆栈跟踪。

答案 1 :(得分:0)

当您输入,时,您将获得InputMismatchException,您首先需要处理用户的输入,因为用户可能会输入任何内容。

try {
    time = input.nextInt();
} catch (Exception e) {
    System.out.println(e.getMessage());
    System.out.println("Your input should be a number!");
    continue;
}

enter image description here