-1

I'm learning java for the first time and am making a program that outputs how much money is in my debit card as well as how much I owe. Here is my code:

public class Main {
    public static void main(String[] args) {
        double bankAccountNumbers = Main.calculateMoneyExpenditures(6350.46, 2700.00, 115.75);
        System.out.printf("Money left in debit card: $%,.2f%n", bankAccountNumbers);
        System.out.printf("Money left before going into debt: $%,.2f%n", bankAccountNumbers);
    }
    public static double calculateMoneyExpenditures(double debitCardMoney, double creditLimit, double amountOfDebt) {
        double moneyInCard = (debitCardMoney);
        return moneyInCard;
        double amountToSpend = (creditLimit);
        return amountToSpend;
        double amountOwed = (creditLimit - amountOfDebt);
        return amountOwed;
    }
}

However, when I compile this code into Repl.it, I get the following error:

enter code here Main.java:11: error: unreachable statement
             double amountToSpend = (creditLimit);
               ^
            Main.java:13: error: unreachable statement
           double amountOwed = (creditLimit - amountOfDebt);
            ^
         2 errors

Can someone please tell me how I can resolve this error for this code and future projects where I have to follow these scoping rules?

sleepToken
  • 1,866
  • 1
  • 14
  • 23
  • 1
    A return is the end of a function, so anything after return is unreachable. A function always return zero or 1 objects, no more. So what is it exactly you would like to calculate? – I.Brok Mar 03 '20 at 17:40
  • I only wanted to calculate the amount owed (e.g. amountOwed) and then return the values that I have declared in the string class above (e.g. debitCard Money and creditLimit) – joey Galileo Hotto Mar 03 '20 at 17:42
  • Consider wrapping these variables into a generic class. As shown here: https://stackoverflow.com/questions/2832472/how-to-return-2-values-from-a-java-method – solid.py Mar 03 '20 at 17:42

2 Answers2

0
public class Main {
    public static void main(String[] args) {
        double debitCardMoney = 6350.46
        double bankAccountNumbers = Main.calculateMoneyExpenditures(2700.00, 115.75);
        System.out.printf("Money left in debit card: $%,.2f%n", debitCardMoney);
        System.out.printf("Money left before going into debt: $%,.2f%n", bankAccountNumbers);
    }
    public static double calculateMoneyExpenditures(double creditLimit, double amountOfDebt) {
        return creditLimit - amountOfDebt;
    }
}

I assume based on your comment reply that this is what you want to do. However the naming doesn't make a lot of sense. Try to describe exactly what everthing is.

Next step would be to create something like account objects instead of using static functions

I.Brok
  • 319
  • 1
  • 2
  • 16
0

In a method in java you cannot access any code after a return. This is your code:

public static double calculateMoneyExpenditures(double debitCardMoney, double creditLimit, double amountOfDebt) {
        double moneyInCard = (debitCardMoney);
        return moneyInCard;
        double amountToSpend = (creditLimit);
        return amountToSpend;
        double amountOwed = (creditLimit - amountOfDebt);
        return amountOwed;
    }

It should be:

public static double calculateMoneyExpenditures(double creditLimit, double amountOfDebt) {
        return creditLimit - amountOfDebt;
    }

After returning moneyInCard, the method is finished and nothing after it will run. Judging by your comments what I think you want to return is the creditLimit - amountOfDebt. I.Brok's answer is correct, but this is why.

Cats4Life
  • 31
  • 7