Java – the interest calculator does not display results
I try to finish this at the last minute of my java class. When I run the program and ask whether the user information is correct, it just loops back to the first question, anyway This is 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); } }
Sample output I'm looking for:
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
etc.
Solution
If the input is wrong, all you may have to do is restart the loop In this case, you only need to switch between continue and interrupt
The continue statement causes the loop to immediately start the next iteration You should delete it with an empty statement; Or negate the if condition, delete else and make it true In addition, your code does nothing else, then takes the input and asks if it is correct Because it is written in a do... While (true) loop, the loop will never end So either delete the loop or add an option to abort the process