jh365 - 1 month ago 16
Java Question

# JAVA: Infinite loop issue

I'm completely new to Java and I have made this program for an assignment in my intermediate class. It is supposed to prompt users for information, and then display it in a table. The program works just as intended except when prompted to answer "y or n", if answered anything besides "y" it loops infinitely.
import java.util.Scanner;

``````public class InterestCalculator
{
public static void main(String[] args)
{

Scanner userInput = new Scanner(System.in);

double startingBalance;
double interestRate;
double endingBalance;
double interestEarned;
char yesOrNo;
int temp;
int numberOfQuarters;

while(true)
{
System.out.println("Enter number of quarters from 1 to 10");
numberOfQuarters = userInput.nextInt();
if (numberOfQuarters > 0 && numberOfQuarters <=10)
{
break;
}
else
{
System.out.println("Number of quarters must be between 1 and 10 inclusive");
}
}

while(true)
{
System.out.println("Enter the beginning principal balance greater than zero");
startingBalance = userInput.nextDouble();
if (startingBalance > 0)
{
break;
}
else
{
System.out.println("Beginning balance must be greater than zero");
}
}

while(true)
{
System.out.println("Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20%"
);
interestRate = userInput.nextDouble();
if (interestRate > 0 && interestRate <= 20)
{
break;
}
else
{
System.out.println("Interest rate must be between 1 and 20 inclusive");
}
}

System.out.println("You entered a principal balance of \$" + startingBalance + " for " + numberOfQuarters + " quarters at " + interestRate + "% interest.");
System.out.println("Is this correct? (y/n)");
yesOrNo = userInput.next().charAt(0);

while(true)
{
if (yesOrNo == 'y' || yesOrNo == 'Y')
{
break;
}
else
{
System.out.println("Please provide the information again");
}
}

System.out.println("Quarter       Beginning       Interest       Ending");
System.out.println("Number        Balance         Earned         Balance");
//here is where I am doing my calculations
for (int i = 0; i < numberOfQuarters; ++i)
{
interestEarned = (startingBalance * (interestRate / 100) * (0.25) );
endingBalance = startingBalance + interestEarned;

System.out.printf((i+1) + "%20.2f %14.2f %15.2f \n", startingBalance, interestEarned, endingBalance);

}
}
}
``````

I am really a noob so I'm sorry for how messy this might be and if its something minor that I should know.

Thank you very much.

Here is the portion specifically:

``````   System.out.println("You entered a principal balance of \$" + startingBalance + " for " + numberOfQuarters + " quarters at " + interestRate + "% interest.");
System.out.println("Is this correct? (y/n)");
yesOrNo = userInput.next().charAt(0);

while(true)
{
if (yesOrNo == 'y' || yesOrNo == 'Y')
{
break;
}
else
{
System.out.println("Please provide the information again");
}
}
``````

I don't know what you're really trying to achieve, but i think you'r missing a variable. You're always making you calculations based on your `startingBalance`, and you never change its value, so you're always getting the same values.

See if this helps.

``````    double currentBalance = startingBalance;

for (int i = 0; i < numberOfQuarters; ++i) {
interestEarned = (currentBalance * (interestRate / 100) * (0.25));
endingBalance = currentBalance + interestEarned;

System.out.printf((i + 1) + "%20.2f %14.2f %15.2f \n", currentBalance, interestEarned, endingBalance);

currentBalance = endingBalance;
}
``````
Source (Stackoverflow)