1

I am currently writing a program that will read through a designated text file that checks the transaction values of each buy/sell/summary and checks the arithmetic such that if the transactions from the buy and sell statements do not equal the total transaction amount that was given in the summary then it outputs an error and closes the program. But currently my method scanMoneyValue has an error that says it's not returning a double, when in fact it is. Is there a different way I should go about returning the values from my method? Here is my code for reference:

    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;

    }

}

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user1860709
  • 23
  • 1
  • 3
  • By the way, 90% of the code here is unnecessary. It'd make a much cleaner question if you picked out the relevant method and put it in a smaller program that gives the same error. – David Z Jan 30 '13 at 21:54
  • In future, please cut your code down to the relevant section (or ideally create a *short* but complete program which demonstrates the problem). You've posted well over 100 lines of code, and only a very small proportion of that code is relevant. – Jon Skeet Jan 30 '13 at 21:55

4 Answers4

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 the if condition fails then there's no return statement. You have a return inside of the condition but not outside. You'll need to add a return statement at the end, or throw an exception if not having a dollar sign is an error.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
5

Okay, looking at the only relevant part of your code:

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;
    }
}

You do return a value if dollar starts with a $... but what do you expect to happen if it doesn't start with $? Currently you reach the end of the method without returning anything, which isn't valid.

You should probably throw an exception, if this is unexpected data that you can't actually handle.

Additionally, you shouldn't really use double for currency values anyway, due to the nature of binary floating point types. Consider using BigDecimal instead.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • +1 for the `BigDecimal` suggestion. Here is a more detailed answer on why not to use floating point types for currency: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Thomas Jungblut Jan 30 '13 at 22:18
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
}

The error you get is because when you write a function all branches of that function must return a value of the correct type. In your case, if the if-statement fails it hits the end of the function without returning anything.

Grambot
  • 4,370
  • 5
  • 28
  • 43
0

Its better to change it on

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;
}

link on explain - Parsing a currency String in java

Community
  • 1
  • 1
iMysak
  • 2,170
  • 1
  • 22
  • 35