# Method Help

• 03-22-2013, 04:22 AM
07.350
Method Help
I'm creating three new methods in my code; getPrincipalAmount (acquire principal amount from the user and validate it) , getInterestRate (acquire interest rate from the user and validate it), getTerm (acquire term length from the user and validate it).

Basically the part I'm confused on is in the method how to ask the user and have them input it and then validate it.Any help would be greatly appreciated, thanks!

Code:

```import java.util.Scanner; public class InterestCalculator5 {         public static void main(String[] args )         {                 Scanner input = new Scanner(System.in);                 String strAnsYesNo;                 char ansYesNo = 0;                 getPrincipalAmount(1);                 getInterestRate(1);                 getTerm(1);         }                                                                                                                                                                                                                                           /** Round **/           public static double round(double numb1, double numb2) {             double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;                 return round;           }         /** Calculate Simple **/           public static double calculateSimpleInterest(double numb1, double numb2, double numb3) {             double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0));                 return calculateSimpleInterest;           }                     /** Calculate Compounded Daily **/           public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) {             double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1;                 return calculateCompoundInterest;           }                     /** display interest **/           public static void displayInterest(double numb1, double numb2, double numb3, String word1, double numb4, double numb5 ) {                                                     System.out.println("Principal Amount    Interest Rate    Term    Calculation Type    Calculated Interest    Total Repayment");                 System.out.println("----------------    -------------    ----    ----------------    -------------------    ---------------");                 System.out.print(numb1+"                ");                 System.out.print(+numb2+"          ");                 System.out.print(+numb3+"          ");                 System.out.print(word1+"            ");                 System.out.print(+numb4+"              ");                 System.out.println(+numb5);           }                     /** Get principal amount **/           public static double getPrincipalAmount(double numb1) {                   Scanner input = new Scanner(System.in);                 do{System.out.print("Enter Loan Amount: ");                   double numb2 = input.nextDouble();                   double getPrincipalAmount = 0;                   if(numb2 > 0)                   getPrincipalAmount = numb2;                                   else{                                                    System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1);                                   }                                          }while (numb2 < 0);                 return getPrincipalAmount;           }                     /** Get interest rate **/           public static double getInterestRate(double numb1) {                   Scanner input = new Scanner(System.in);                   do{System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");                     double numb2 = input.nextDouble();                   double getInterestRate = 0;                 {if (numb2 >= 0 && numb2 <= 100)                   getInterestRate = numb2;                                   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 " +numb1);                                   }                                          }while (numb2 <= 0 && numb2 >= 100);                 return getInterestRate;           }                     /** Get term **/           public static double getTerm(double numb1) {                   Scanner input = new Scanner(System.in);                   do{System.out.print("Enter the Term (in months): ");                     double numb2 = input.nextInt();                   double getTerm = 0;                   if (numb2 > 0)                   getTerm = numb2;                                   else{                                                    System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1);                                   }                                          }while (numb2 < 0);                 return getTerm;           }                     /** Get calculation type **/           public static double getCalculationType(double numb1) {                   Scanner input = new Scanner(System.in);                   System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):");                     int numb2 = input.nextInt();                   double getCalculationType = 0;                   do{if (numb2 <1||numb2 >3)                           getCalculationType = numb2;                                   else{                                                    System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb1);;                                   }                                          }while (numb2 > 1||numb2 < 3);                 return getCalculationType;           } }```
I'm getting the error, I'm not sure if I have the do loop in the wrong spot in the methods or what?

Code:

```Exception in thread "main" java.lang.Error: Unresolved compilation problems:         numb2 cannot be resolved to a variable         getPrincipalAmount cannot be resolved to a variable         at InterestCalculator5.getPrincipalAmount(InterestCalculator5.java:69)         at InterestCalculator5.main(InterestCalculator5.java:15)```
• 03-22-2013, 04:25 AM
Junky
Re: Method Help
You have declared the variable numb2 inside the do/while loop and therefore cannot use it outside the loop. In a do/while loop the condition is deemed to be outside the loop.
• 03-22-2013, 05:34 AM
Junky
Re: Method Help
Method Help

Duplicate post