如果传入无效值,则抛出异常

时间:2015-12-05 22:10:45

标签: java exception throw

我有一个类,现在我正在更改setter以在传入无效值时抛出异常。 它要求:

  • 甲。 (dueDay必须介于1到31之间,dueMonth必须介于1到12之间。)异常显示不在setter方法中处理。
  • B中。更改TodoItem的主要方法,以便它向用户询问任务,截止日期和截止月份,并将此信息存储为新的TodoItem
  • ℃。更改构造函数,以便将其称为新的setter方法。如果抛出异常,则应在main方法中处理。应告知用户他们输入了无效的日期或月份,并要求提供正确的日期或月份。

我的课程是:

(我已经更改了setter以抛出异常,但是,它不起作用,我想我应该更改main函数的构造函数,但是我不知道该怎么做。)

public class TodoItem {

    private String task;
    private int dueMonth;
    private int dueDay;
    private boolean isDone;

    // class variables
    private static int numItems;
    private static int numDone;

    // constructor
    public TodoItem(String taks,int day,int month) {
        this.task = task;
        dueDay = day;
        dueMonth = month;
        isDone = false;

        numItems++;
    }

    // second constructor
    public TodoItem(String task) {
        this.task = task;
        isDone = false;

        numItems++;
    }

    public static void WriteToFile(String a){
        a="toString.txt";
        String task;
        int dueMonth;
        int dueDay;
        boolean isDone;
    }
    // toString method
    public String toString() {
        return a+task + ", due: " + dueMonth + "/" + dueDay + (isDone?", done":", todo");
    }

    // getters
    public int getDueDay() {
        return dueDay;
    }

    public int getDueMonth() {
        return dueMonth;
    }

    // setters
    public void setDueDay(int day) throws Exception  {
        if (day >= 1 && day <=31) {
            dueDay = day;
        }
        else {
            throw new Exception ("Error:  invalid due day");
        }
    }

    public void setDueMonth(int month) throws Exception{
        if (month >= 1 && month <= 12) {
            dueMonth = month;
        }
        else {
            throw new Exception("Error:  invalid due month");
        }
    }

    // Checks off an item as being done.
    // If the item was already marked as done, don't increase the counter
    // (this was not specified in the problem set instructions).
    public void markAsDone() {
        if (!isDone) {
            isDone = true;
            numDone++;
        }
    }


    // returns the percentage of to-do list items completed
    public static double percentDone() {
        return (double) numDone/numItems*100;
    }

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        // constructor 1
        TodoItemDone item1 = new TodoItemDone("Walk dog",12,3);
        TodoItemDone item2 = new TodoItemDone("Do 326 project",16,3);
        TodoItemDone item3 = new TodoItemDone("Put away winter boots",21,3);

        // constructor 2
        TodoItemDone item4 = new TodoItemDone("Buy groceries");
        TodoItemDone item5 = new TodoItemDone("Clean bathroom");
        TodoItemDone item6 = new TodoItemDone("Study for myself");


        // toString (and verify constructors)
        System.out.println("The 6 items are:");
        System.out.println(item1);
        System.out.println(item2);
        System.out.println(item3);
        System.out.println(item4);
        System.out.println(item5);
        System.out.println(item6);

        System.out.println();
        System.out.println("Setting due dates and months on the last 3:");
        // setDueDay
        item4.setDueDay(1);
        item5.setDueDay(5);
        item6.setDueDay(52);
        // setDueMonth
        item4.setDueMonth(12);
        item5.setDueMonth(6);
        item6.setDueMonth(0);

        System.out.println("The last 3 items are now:");
        System.out.println(item4);
        System.out.println(item5);
        System.out.println(item6);

        // Test percentDone() and markAsDone()
        System.out.println();
        System.out.println("About to complete some items:");
        System.out.println("percent done: " + percentDone());
        item1.markAsDone();
        System.out.println("Item 1 is now: " + item1);
        System.out.println("percent done: " + percentDone());

        item1.markAsDone();
        System.out.println("Item 1 is now: " + item1);
        System.out.println("percent done: " + percentDone());

        item2.markAsDone();
        System.out.println("Item 2 is now: " + item2);
        System.out.println("percent done: " + percentDone());
    }

2 个答案:

答案 0 :(得分:1)

更改此部分:

public TodoItem(String taks,int day,int month) {
    this.task = task;
    dueDay = day;
    dueMonth = month;
    isDone = false;

    numItems++;
}

到此:

public TodoItem(String task,int day,int month) {
    this.task = task;
    dueDay = day;
    dueMonth = month;
    isDone = false;

    numItems++;
}

您没有将this.task设置为更正输入参数taks。 我改变你的论点名称。 也许这不是主要问题,但试着改变它。

答案 1 :(得分:1)

  但是,它没有用,

您定义了TodoItem类,但在main()中您创建了TodoItemDone。当我将TodoItem更改为TodoItemDone时,我有结果:

The 6 items are:
null, due: 3/12, todo
null, due: 3/16, todo
null, due: 3/21, todo
Buy groceries, due: 0/0, todo
Clean bathroom, due: 0/0, todo
Study for myself, due: 0/0, todo

Setting due dates and months on the last 3:
Exception in thread "main" java.lang.Exception: Error:  invalid due day
    at com.github.vedenin.TodoItemDone.setDueDay(TodoItemDone.java:61)
    at com.github.vedenin.TodoItemDone.main(TodoItemDone.java:120)

正确抛出