Why does my code keep return an infinite number of Current Balance: 0?

Advertisements

I know I’m missing something really important here. Something tells me the code is redundant. When I execute the below code,

package sct;

public class Account {

private static int balance;
public Account(String Account, double d) {
    System.out.println("Intial state");
    return;
}
public static Object displayBalance(int balance){
    System.out.println("Current Balance: " + balance);
    return displayBalance(balance);}
static Account usAccount = new Account("US account", 100.00);
static Account chAccount = new Account("Swiss account", 1000000.00);    

public static int amountWithdrawing(int balance, int withdrawAmount)
    {   System.out.println("Withdraw Amount: " + withdrawAmount);
return amountWithdrawing(balance, withdrawAmount);
}   
public static int amountDepositing(int balance, int depositAmount) {
System.out.println("Depositing Amount: " + depositAmount);
return balance = balance + depositAmount;
}
public static void main(String[] args) { 
    System.out.println("Withdrawal Amount " + 20);
    System.out.println("Deposit Amount " + 200);
    System.out.println("Ending balances");
    displayBalance(balance);
    balance = amountWithdrawing(100, 20);
    displayBalance(balance);
    balance = amountDepositing(1000000, 200);
    System.out.println("usAccount Current Balance: " + usAccount);
    System.out.println("chAccount Current Balance: " + chAccount);}
 }

I keep getting the below in console:

Current Balance: 0
infinite loop

Exception in thread "main" java.lang.StackOverflowError
at java.base/java.lang.System$2.encodeASCII(System.java:2489)
at           java.base/sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(SingleByte.java:220)
at java.base/sun.nio.cs.SingleByte$Encoder.encodeLoop(SingleByte.java:276)
at     java.base/java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:585)
at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:374)
at java.base/sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:361)
at java.base/sun.nio.cs.StreamEncoder.lockedWrite(StreamEncoder.java:162)
at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:143)
at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:220)
at java.base/java.io.BufferedWriter.implFlushBuffer(BufferedWriter.java:178)
at java.base/java.io.BufferedWriter.flushBuffer(BufferedWriter.java:163)
at java.base/java.io.PrintStream.implWriteln(PrintStream.java:848)
at java.base/java.io.PrintStream.writeln(PrintStream.java:826)
at java.base/java.io.PrintStream.println(PrintStream.java:1168)
at sct.Account.displayBalance(Account.java:11)
at sct.Account.displayBalance(Account.java:12)
infinite loop

I’m an epic noob. this is my third time writing code so I don’t know what I’m doing wrong. any help would be appreciated!

>Solution :

The issue is in this line:

public static Object displayBalance(int balance){
    System.out.println("Current Balance: " + balance);
    return displayBalance(balance);}

You are declaring a function "displayBalance". In this function, you print out the current balance that was put into the function, then return…by calling displayBalance again, which prints again, then calls displayBalance again, which prints again, then…

See the problem?

If you want this function to print and then exit, simply change the "public static Object" line to "public static void", and replace "return displayBalance(balance);" at the end with "return;".

You should fix your other functions with the same method, because they will also loop (recurse) infinitely. Google "recursion" to learn more about how this type of infinite loop works, and welcome to Stack Overflow!

Leave a ReplyCancel reply