jh365 jh365 - 2 months ago 12
Java Question

Interest calculator not displaying results

I'm trying to finish this at the last minute for my Java class, when I run the program after it asks the user if the information is correct, it just loops back to the first question no matter what. Here's the code:

import java.util.Scanner;

public class InterestCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userResponse = null;

do {
int quartersDisplayed = -1;
double startingBalance = -1,
interestRate = -1;

do {
System.out.println("Enter the numbers of quarters you wish to display that is greater than zero and less or equal to 10: ");
userResponse = input.next();

try{
quartersDisplayed = Integer.parseInt(userResponse);
} catch(NumberFormatException e) {

}

if(quartersDisplayed <= 0 || quartersDisplayed > 10) {
System.out.println("Sorry, that value is not valid.");
} else {
break;
}
} while(true);


do {
System.out.println("Enter the starting balance (must be greater than zero): ");
userResponse = input.next();

try {
startingBalance = Double.parseDouble(userResponse);
} catch(NumberFormatException e) {

}

if(startingBalance <= 0) {
System.out.println("Sorry, that value is not valid.");
} else {
break;
}
} while(true);


do {
System.out.println("Enter the interest rate (greater than zero less than twenty percent): ");
userResponse = input.next();

try {
interestRate = Double.parseDouble(userResponse);
} catch(NumberFormatException e) {

}

if(interestRate <= 0 || interestRate > 20){
System.out.println("Sorry, that value is not valid.");
} else {
break;
}
} while(true);


System.out.println("You have entered the following amount of quarters: "
+ quartersDisplayed);
System.out.println("You also entered the starting balance of: " + startingBalance);
System.out.println("Finally, you entered the following of interest rate: "
+ interestRate);
System.out.println("If this information is not correct, please exit the program and enter the correct information.");

double quarterlyEndingBalance = startingBalance + (startingBalance * interestRate / 100 * .25);
System.out.println("Your ending balance for your quarters is "
+ quarterlyEndingBalance);
System.out.println("Do you want to continue?");
userResponse = input.next();

if("y".equalsIgnoreCase(userResponse) || "yes".equalsIgnoreCase(userResponse))
continue;
else
break;

} while(true);
}
}


What I am looking for as a sample output:

Enter number of quarters from 1 to 10
5
Enter the beginning principal balance greater than zero
4500
Enter the interest rate percentage without the percent sign, greater than 0 percent and less than/equal to 20%
3.5
You entered a principal balance of $4500.0 for 5 quarters at 3.5% interest.
Is this correct? (y/n)
y
Quarter Beginning Interest Ending
Number Balance Earned Balance
1 4500.00 39.38 4539.38


ect ect

Answer

Maybe what you ment to do is to restart the loop, if the input was wrong. In this case, you just need to switch the continue and the break.

The continue statement makes a loop immediately start the next iteration. You shoud either remove it by an empty statement ;, or negate the if-condition, remove the else and make it break on true. In addition, your code does nothing else, then getting the input and asking whether it is correct. And as it is written inside a do...while(true) loop, this loop will never end. So either remove that loop, or add an option to abort the process.