Results 1 to 8 of 8
Thread: Boolean help
 02142013, 05:01 PM #1Member
 Join Date
 Feb 2013
 Posts
 7
 Rep Power
 0
Boolean help
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!!
Java Code: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; } } }
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!
 02142013, 05:05 PM #2
Re: Boolean help
Use a List of Strings and add your error messages to that.
Math problems? Call 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
 02142013, 05:20 PM #3Member
 Join Date
 Feb 2012
 Posts
 59
 Rep Power
 0
Re: Boolean help
if you want it to only display that line when there is no error set a boolean to false, and then have the line tied to that boolean. basic boolean example in pseudocode:
Java Code:boolean test=false; if(error){ display errorMsg; test=true; } if (test==false){ ask operation type; }
WARNING: Copying this "code" may cause compiler malfunction, and by may I mean definitely will.Last edited by lenois; 02142013 at 05:24 PM.
 02142013, 05:23 PM #4Member
 Join Date
 Feb 2012
 Posts
 59
 Rep Power
 0
Re: Boolean help
Also of note I have heard that it is preferable to write switches in methods so that they can be stoped with a return. It works as well as the breaks, but avoids the hated breaks. Both work, it is just preference.
 02142013, 05:32 PM #5Member
 Join Date
 Feb 2013
 Posts
 7
 Rep Power
 0
Re: Boolean help
I'm still confused on how I'd write that as a list of strings to get it to work
 02142013, 06:04 PM #6Member
 Join Date
 Feb 2012
 Posts
 59
 Rep Power
 0
Re: Boolean help
seemed to work fine when i did it. although you had a typo on line 82.
i would still use a boolean to test so that you don't get that line to print
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 25
Enter the Term (in months): 12
Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):2
Total calculated interest: 28.073156065712197
Total amount to be repaid: 128.0731560657122
 02142013, 06:06 PM #7Member
 Join Date
 Feb 2012
 Posts
 59
 Rep Power
 0
Re: Boolean help
it would work with an array but it is fine now
 02142013, 06:51 PM #8Member
 Join Date
 Feb 2012
 Posts
 59
 Rep Power
 0
Re: Boolean help
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 25
Enter the Term (in months): 1
Data Error: Term must be greater than zero. You entered 1
Calculation Type Error: You must select 1, 2 or 3. You entered 0
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 12
Enter the Term (in months): 12
Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered 12.0
Calculation Type Error: You must select 1, 2 or 3. You entered 0
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 12
Enter the Term (in months): 12
Data Error: Loan amount must be greater than zero. You entered 100
Calculation Type Error: You must select 1, 2 or 3. You entered 0
that was fixed by changing your flow of control, check your else statements.
the bottom error is because of where the switch is.
edit: i moved the switch
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 25
Enter the Term (in months): 12
Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered 25.0
jGRASP: operation complete.
jGRASP exec: java InterestCalculator2
Enter Loan Amount: 100
Enter Yearly Interest Rate (1 to 100 percent): 25
Enter the Term (in months): 12
Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):1
Total calculated interest: 25.0
Total amount to be repaid: 125.0
I am not giving you the code, I am telling you it works with a few simple fixes.
step 1 check your elses
step 2 figure out where the switch needs to beLast edited by lenois; 02142013 at 06:55 PM.
Similar Threads

Boolean
By must0995 in forum New To JavaReplies: 1Last Post: 04132012, 10:18 PM 
Boolean
By Twixmaster123 in forum New To JavaReplies: 1Last Post: 04132012, 08:20 AM 
Boolean help
By FadedAura in forum New To JavaReplies: 2Last Post: 11202011, 02:56 PM 
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09132011, 12:01 AM 
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05052011, 09:04 AM
Bookmarks