从方法中返回一个double

时间:2013-01-30 21:49:48

标签: java double return

我目前正在编写一个程序,该程序将读取指定的文本文件,该文件检查每个买/卖/摘要的交易值,并检查算术,以便买卖报表中的交易不等于总交易在摘要中给出的金额然后输出错误并关闭程序。但是目前我的方法scanMoneyValue有一个错误,表示它没有返回一个double,实际上它是。我应该采用不同的方式从我的方法中返回值吗?这是我的参考代码:

    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 个答案:

答案 0 :(得分: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语句,或者如果没有美元符号则抛出异常。

答案 1 :(得分: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

答案 2 :(得分: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语句失败,它会在函数结束时返回而不返回任何内容。

答案 3 :(得分: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;
}

链接解释 - Parsing a currency String in java