Java:2个运行时错误我无法弄清楚

时间:2012-02-10 23:48:53

标签: java

我正在完成一项家庭作业,而且我的任务“超出了”作业要求的内容。我的代码中出现了运行时错误,并且在我的生活中无法弄清楚我做错了什么。

这是作业:

  

编写一个显示模拟薪水的程序。程序应该要求用户输入日期,收款人姓名和支票金额。然后它应该显示一个带有拼写的美元金额的模拟支票。

这是我的代码:

CheckWriter:

/* CheckWriter.java */

// Imported Dependencies
import java.util.InputMismatchException;
import java.util.Scanner;

public class CheckWriter {
    public static void main(String args[]) {
        Scanner keyboard = new Scanner(System.in);

        // Try to get the name
        String name = "";
        NameValidator validateName = new NameValidator();
        while (validateName.validate(name) == false) {
            System.out.println("Enter the name: ");
            name = keyboard.nextLine();

            if (validateName.validate(name) == false) {
                System.out.println("Not a valid name.");
            }
        }

        // Get the date
        String date = "";
        DateValidator validateDate = new DateValidator();
        while (!validateDate.validate(date)) {
            System.out.println("Enter the date (dd/mm/yyyy): ");
            date = keyboard.nextLine();

            if (!validateDate.validate(date)) {
                System.out.println("Not a valid date.");
            }
        }

        // Try to get the amount of the check
        String checkAmount = "";
        CurrencyValidator validateCurrency = new CurrencyValidator();
        while (!validateCurrency.validate(checkAmount)) {

            System.out.print("Enter the Check Amount (XX.XX):  $");
            checkAmount = keyboard.nextLine();

            if (!validateCurrency.validate(checkAmount)) {
                System.out.println("Not a valid check amount.");
            }
        }

        String checkWords = checkToWords(checkAmount);  // ERROR! (48)

        System.out
        .println("------------------------------------------------------\n"
                + "Date: "
                + date
                + "\n"
                + "Pay to the Order of: "
                + name
                + "         $"
                + checkAmount
                + "\n"
                + checkWords
                + "\n"
                + "------------------------------------------------------\n");
    }

    private static String checkToWords(String checkAmount) {

        /**
         * Here I will use the string.split() method to separate out
         * the integer and decimal portions of the checkAmount.
         */

        String delimiter = "\\.\\$";
        /* Remove any commas from checkAmount */
        checkAmount.replace(",", "");
        /* Split the checkAmount string into an array */
        String[] splitAmount = checkAmount.split(delimiter);

        /* Convert the integer portion of checkAmount to words */
        NumberToWords intToWord = new NumberToWords();
        long intPortion = Long.parseLong(splitAmount[0]);       // ERROR! (84)
        intToWord.convert(intPortion);
        String intAmount = intToWord.getString() + " dollars";

        /* Convert the decimal portion of checkAmount to words */
        String decAmount = "";
        long decPortion = Long.parseLong(splitAmount[1]);
        if (decPortion != 0) {
            NumberToWords decToWord = new NumberToWords();
            decToWord.convert(Long.parseLong(splitAmount[1]));
            decAmount = " and " + decToWord.getString() + " cents.";

        }

        return (intAmount + decAmount);
    }
}

请注意,我正在使用外部类文件来处理名称,日期,货币以及从数字到单词的转换的验证。这些类文件都按预期工作。

我得到的错误是:

  

线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“”

at java.lang.NumberFormatException.forInputString(Unknown Source)

at java.lang.Long.parseLong(Unknown Source)

at java.lang.Long.parseLong(Unknown Source)

at CheckWriter.checkToWords(CheckWriter.java:82)

at CheckWriter.main(CheckWriter.java:46)

我已经在代码中注释了导致我遇到错误的行。

有人可以帮我弄清楚我的代码出错了吗?如果您认为需要,我可以包含其他类文件。

编辑:当我运行代码时,它会询问名称和日期。在要求支票金额之前,它会抛出错误。

编辑2:非常感谢cotton.m!感谢他的建议,我将while语句更改为:

while(!validateDate.validate(date) && date == "")

这已经解决了我的问题。看来,当使用正则表达式验证数据时,空字符串将返回true。

4 个答案:

答案 0 :(得分:2)

是NumberFormatException,checkAmount(method参数)中的值不是有效的数字。

您需要设置checkAmount=checkAmount.replace(",", "");

否则checkAmount仍然会在里面并导致NumberFormatExcpetion。

答案 1 :(得分:2)

您的问题在于您的分隔符正则表达式,目前您使用的是\.\$,它将分为文字.,后跟文字$。我假设您实际打算做的是拆分 一个.$,所以将分隔符更改为以下之一:

String delimiter = "\\.|\\$"

String delimiter = "[\\.\\$]"

正如您的代码现在,checkAmount.split(delimiter)实际上并未成功地将字符串拆分到任何位置,因此Long.parseLong(splitAmount[0])相当于Long.parseLong(checkAmount)

答案 2 :(得分:2)

您尝试在空长度字符串中解析的字符串。

我的建议是

1)检查checkToWords开头的checkAmount的值 - 如果它是空白则是你的问题

2)不要那样做。只需更换你所做的$,(我认为这是你真正的问题)

此外,您将遇到另一个问题,即10000.00不长。我看到你正在分裂。但这真的是你想要的吗?

答案 3 :(得分:0)

应该是:

String delimiter = "[\\.\\$]";

然后你必须检查splitWord [i]是否为空。