如果在命令行中没有放入任何内容,如何打印错误?

时间:2015-11-19 04:44:24

标签: java indexoutofboundsexception

如果在java Rev之后没有输入任何内容,我需要打印此语句"只输入一个三位数字,第一个数字大于第三个数字。"

这不会被打印,但会出现一个错误:

x-10-104-10-64:Assgn Katie$ javac Rev.java
x-10-104-10-64:Assgn Katie$ java Rev
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Rev.main(Rev.java:9)

我需要修理什么?

这是我的代码:

public class Rev {  
    public static void main(String[] args) {
        int num = Integer.parseInt(args[0]);
        if (checkDigits(num)) {
            num = subtractNum(num);
            addNum(num);
        } else {
            System.out.println("Enter only a three digit number, with the first digit larger than the third");
        }
    }

    // checks if numbers are correct
    static boolean checkDigits(int number) {
        int reverse = reverseNum(number);
        if(number < reverse) {
            throw new Error("Reverse number needs to be less than the original number!");
        } else {
            return true;
        }
    }

    //reverses number
    static int reverseNum(int number) {
        int reverse = 0;
        while(number != 0) {
            reverse = (reverse*10)+(number%10);
            number = number/10;
        }
        return reverse;
    }

    // subtracts
    static int subtractNum(int number) {
        int reverse = reverseNum(number);
        int total = number - reverse;
        System.out.println("Reverse and subtract: ");
        System.out.println(number);
        System.out.println(reverse + " - ");
        System.out.println("---");
        System.out.println(total);
        System.out.println();
        return total;
    }

    // adds
    static int addNum(int number) {
        int reverse = reverseNum(number);
        int total = number + reverse;
        System.out.println("Reverse and add: ");
        System.out.println(number);
        System.out.println(reverse + " + ");
        System.out.println("---");
        System.out.println(total);
        return total;
    }
}

3 个答案:

答案 0 :(得分:1)

检查args的长度。添加类似

的内容
if (args.length != 1) {
    System.err.println("Please provide a single argument.");
    System.exit(1);
}

main的开头。

答案 1 :(得分:0)

您正在尝试访问args数组的第0个元素,该元素可能不存在(如果程序是在没有参数的情况下运行的)。您应该在尝试访问该元素之前验证ArrayIndexOutOfBoundsException是否有足够的长度,或者使用try / catch块处理var weekDayLabels:Array = new Array("Sunday","Monday", "Tuesday","Wednesday","Thursday","Friday","Saturday"); var someBirthday:Date = new Date(1974, 10, 30, 1, 20); trace(someBirthday); // Sat Nov 30 01:20:00 GMT-0800 1974 trace(someBirthday.getDay()); // 6 trace(weekDayLabels[someBirthday.getDay()]); // Saturday

答案 2 :(得分:0)

在catch部分内使用带有print语句的try / catch块来显示该特定消息。

public static void main(String[] args)
{
    try {

        int num = Integer.parseInt(args[0]);
        if (checkDigits(num))
        {
            num = subtractNum(num);
            addNum(num);
        }

    } catch (Exception e) {
            System.out.println("Enter only a three digit number, with the first digit larger than the third");
        }
}