Hey, I'm stuck on my current code, interest calculator, to solve for simple, monthly compounded, and daily compounded interest.
If one error occurs I need it to state the error, if two errors occur I need it to state them both, and so on..
Right now if I have an error on any of them my last error is the one that pops up.. Any help would be great!!
import java.util.Scanner; public class InterestCalculator2{ public static void main(String[] args ) { Scanner input = new Scanner(System.in); //Prompt user to enter loan amount System.out.print("Enter Loan Amount: "); int principle = input.nextInt(); //Prompt user to enter interest rate System.out.print("Enter Yearly Interest Rate (1 to 100 percent): "); double interest = input.nextDouble(); //Prompt user to enter term System.out.print("Enter the Term (in months): "); int term = input.nextInt(); double simple= principle*(interest/100.0)*(term/12.0); double repaidSimple= simple+principle; double a = 1.0+((interest/100)/365.0); double b = (365*(term/12.0)); double daily = Math.pow(a,b); double dailyCompound = (principle*daily)-principle; double repaidDaily = dailyCompound + principle; double c = 1.0+((interest/100)/12.0); double d = (12*(term/12.0)); double monthly = Math.pow(c, d); double monthlyCompound = (principle*monthly)-principle; double repaidMonthly = monthlyCompound + principle; int type = 0; if (principle > 0) { if (interest > 0 && interest < 100) { if(term > 0) { switch(type) { case 1: break; case 2: break; case 3: break; default: break; } } else { System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest); } } else { System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle); } } else { System.out.println("Data Error: Term must be greater than zero. You entered " +term); } //Prompt user to enter calculation type System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); type = input.nextInt(); switch(type) { case 1:System.out.println("Total calculated interest: " +simple); System.out.println("Total amount to be repaid: " +repaidSimple); break; case 2:Sprintln("Total calculated interest: " +monthlyCompound); System.out.println("Total amount to be repaid: " +repaidMonthly); break; case 3:System.out.println("Total calculated interest: " +dailyCompound); System.out.println("Total amount to be repaid: " +repaidDaily); break; default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break; } } }
Here is the output I get
Enter Loan Amount: 0
Enter Yearly Interest Rate (1 to 100 percent): 100.1
Enter the Term (in months): 12
Data Error: Term must be greater than zero. You entered 12
Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):
As you can see thats the wrong error I need to be outputted and I cant figure out how to keep it from asking "Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):" when an error has occured!
This is how the output should look:
Enter loan amount: 0
Enter yearly interest rate (0 to 100 percent): 100.1
Enter the term (in months): 12
Data Error: Loan amount must be greater than zero. You entered '0'.
Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100.
You entered '100.1'.
Thanks for the help!


LinkBack URL
About LinkBacks
Reply With Quote