1

我目前正在编写一个程序,该程序将读取指定的文本文件,该文件检查每个买入/​​卖出/摘要的交易价值并检查算法,如果买卖语句中的交易不等于总交易金额在摘要中给出然后它输出一个错误并关闭程序。但是目前我的方法scanMoneyValue有一个错误,说它没有返回双精度,而实际上它是。我应该以不同的方式从我的方法中返回值吗?这是我的参考代码:

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;

    import javax.swing.JFileChooser;
    import javax.swing.JOptionPane;



    public class RecurrsionFileChecker {

public static void main(String[] args) {
    int result;

    //File Chooser Window
    JFileChooser chooser = new JFileChooser("/home/nick/workspace/CS 1410-001/src/assignment03");
    chooser.setDialogTitle("Please choose a file to be checked");
    result = chooser.showOpenDialog(null);
    //User Cancelled the chooser
    if (result == JFileChooser.CANCEL_OPTION)
        return;
    File inputfile = chooser.getSelectedFile();

    try
    {   
        Scanner in = new Scanner(inputfile);
        //Call Method to look at next transaction
        scanNextTransaction(in);

    }
    catch (IOException e)
    {   
        System.out.println("Could not read file: " + inputfile);
    }



}



 /**
 * Returns double if the parameter Scanner has an error that does,
 * not match the summary before it.
 *   
 * @param s Any scanner
 * @return double if Summaries don't match.
 */
    public static double scanNextTransaction(Scanner s)
    {   
        String buy, sell, summary, date;
        double amount = 0, referenceValue, total = 0;
        summary = s.next();
        date = s.next();
        referenceValue = scanMoneyValue(s);

        while (s.hasNext())
        {   
            if (s.next() == "Buy")
            {
                date = s.next();
                amount = scanMoneyValue(s);
            }

            if(s.next() == "Sell")
            {
                date = s.next();
                amount = scanMoneyValue(s);
            }

            if(s.next() == "Summary")
            {
                amount = scanSubSummary(s);
            }
            //add the transactions
            total = total + amount;
        }
    return total;

    }



    public static double scanMoneyValue(Scanner in)
    {   
        String dollar = in.next();
        if(dollar.charAt(0) == '$')
        {   //convert string to a double
            String amount = dollar.substring(1);
            double complete = Double.parseDouble(amount);
            complete = complete * 100;
            return complete;
        }
    }


    public static double scanSubSummary(Scanner sub)
    {   
        String summaryDate, transDate, transType;
        int summarySubEntries, count = 0;
        double transValue, summaryValue = 0, totalValue = 0,  summaryAmount;

        summaryDate = sub.next();
        summaryAmount = scanMoneyValue(sub);
        summarySubEntries = sub.nextInt();

        while (count != summarySubEntries)
        {
            transType = sub.next();
            if (transType == "Summary")
            {
                summaryValue = scanSubSummary(sub);
            }
            transValue = scanMoneyValue(sub);   
            totalValue = transValue + totalValue + summaryValue;
            count++;            
        }
        if (totalValue != summaryAmount)
        {
            System.out.print("Summary error on " + summaryDate + ".");
            System.out.println("Amount is $" + summaryAmount + ", " + "should be $" + totalValue + ".");
        }
        return totalValue;

    }

}

4

4 回答 4

5

好的,查看代码中唯一相关的部分:

public static double scanMoneyValue(Scanner in)
{   
    String dollar = in.next();
    if(dollar.charAt(0) == '$')
    {   //convert string to a double
        String amount = dollar.substring(1);
        double complete = Double.parseDouble(amount);
        complete = complete * 100;
        return complete;
    }
}

如果以 ... 开头,您确实会返回一个值,但是如果它以开头,您期望会发生什么?当前,您到达方法的末尾而不返回任何内容,这是无效的。dollar$$

如果这是您实际上无法处理的意外数据,您可能应该抛出异常。

double此外,由于二进制浮点类型的性质,无论如何您都不应该真正使用货币值。考虑BigDecimal改用。

于 2013-01-30T21:53:05.753 回答
5
public static double scanMoneyValue(Scanner in)
{   
    String dollar = in.next();
    if(dollar.charAt(0) == '$')
    {   //convert string to a double
        String amount = dollar.substring(1);
        double complete = Double.parseDouble(amount);
        complete = complete * 100;
        return complete;
    }
}

如果if条件失败,则没有return语句。你有一个return内部条件,但没有外部条件。您需要return在末尾添加一条语句,或者如果没有美元符号是错误的,则抛出异常。

于 2013-01-30T21:52:42.443 回答
0

最好改一下

public static double scanMoneyValue(Scanner in)
{   
    String dollar = in.next();
    String amount = dollar.replaceAll("[^\\d.]+", "")
    double complete = Double.parseDouble(amount);
    complete = complete * 100;
    return complete;
}

解释链接 -在 java 中解析货币字符串

于 2013-01-30T21:59:00.337 回答
0
public static double scanMoneyValue(Scanner in)
{   
    String dollar = in.next();
    if(dollar.charAt(0) == '$')
    {   //convert string to a double
        String amount = dollar.substring(1);
        double complete = Double.parseDouble(amount);
        complete = complete * 100;
        return complete;
    }
    //NEED RETURN STATEMENT HERE
}

您得到的错误是因为当您编写函数时,该函数的所有分支都必须返回正确类型的值。在您的情况下,如果 if 语句失败,它会到达函数的末尾而不返回任何内容。

于 2013-01-30T21:53:59.347 回答