# Boolean help

• 02-14-2013, 05:01 PM
07.350
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!!

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;             }                    }           }```
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!
• 02-14-2013, 05:05 PM
PhHein
Re: Boolean help
Use a List of Strings and add your error messages to that.
• 02-14-2013, 05:20 PM
lenois
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:
Code:

```boolean test=false; if(error){ display errorMsg; test=true; } if (test==false){ ask operation type; }```
that type of logic would stop the line you don't want executing from executing.

WARNING: Copying this "code" may cause compiler malfunction, and by may I mean definitely will.
• 02-14-2013, 05:23 PM
lenois
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.
• 02-14-2013, 05:32 PM
07.350
Re: Boolean help
I'm still confused on how I'd write that as a list of strings to get it to work
• 02-14-2013, 06:04 PM
lenois
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
• 02-14-2013, 06:06 PM
lenois
Re: Boolean help
it would work with an array but it is fine now
• 02-14-2013, 06:51 PM
lenois
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.