# Java Method Help

Printable View

• March 13th, 2013, 06:30 PM
07.350
Java Method Help
I'm trying to create a method for the display interest and I'm struggling with how to come up with how to get it to work. Any help would be great!

Code java:

``` import java.util.Scanner; public class InterestCalculator4 { public static void main(String[] args ) { Scanner input = new Scanner(System.in);   String strAnsYesNo; char ansYesNo = 0;   do{   //Prompt user to enter loan amount System.out.print("Enter Loan Amount: "); double 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): "); double term = input.nextInt();   if (principle > 0) { if (interest >= 0 && interest <= 100) { if(term > 0) { int type = 0;   //Prompt user to enter calculation type do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):"); type = input.nextInt();     switch(type) { case 1: break;   case 2: break;   case 3: break;   default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break;   } }while(type<1||type>3); } else { System.out.println("Data Error: Term must be greater than zero. You entered " +term);   } } 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); }   // Clear out buffer input.nextLine();   do{ System.out.println("Calculate another loan interest (Yes/No)?"); strAnsYesNo = input.nextLine();   // Make sure user entered in data if (strAnsYesNo.length() > 0) ansYesNo = strAnsYesNo.charAt(0);   if(ansYesNo!='y'&& ansYesNo!='Y'&& ansYesNo!='n'&& ansYesNo!='n') { System.out.println("Please enter 'Yes' or 'No'?"); }   }while(ansYesNo!='y'&& ansYesNo!='Y'&& ansYesNo!='n'&& ansYesNo!='n');     }while (ansYesNo =='y' || ansYesNo =='Y'); System.out.println("Thank you for using Interest Calculator!"); }     /** 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 char displayInterest(double numb1, double numb2, double numb3, char word1, double numb4, double numb5 ) { numb5 = numb1 + numb4;   System.out.println("Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment"); System.out.println("---------------- ------------- ---- ---------------- ------------------- ---------------"); System.out.println("+numb1", "+numb2", "numb3", "+word1", "+numb4", "+numb5"); } }```
• March 13th, 2013, 06:33 PM
Norm
Re: Java Method Help
Quote:

how to come up with how to get it to work
Can you describe what the method is supposed to do?
What happens now when you compile and execute the code?
Copy the output and paste it here and explain what is wrong with it.
• March 13th, 2013, 06:42 PM
07.350
Re: Java Method Help
Sorry!
Purpose: To display the result of the calculated interest to the user. Parameters: You will need to determine the number of parameters and their appropriate data type of each parameter to display the output to the user.

It is suppose to print out
Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment Amount
---------------- ------------- ---- ---------------- ------------------- ----------------------
\$1375.0 1.175 7 Compound Monthly \$9.45221 \$1384.45

I was thinking with the parameters
numb1= principle
numb2= interest
numb3=term
word1= the type of interest calculated
numb4= the interest
numb5= the total repayment(interest+principle)

--- Update ---

When the user enters in the principle, interest, and term. Then the user enters in which interest type they'd like to calc so 1-simple 2-monthly compounded 3-daily compounded and when they enter 1,2, or 3 it prints out numbers will vary though to which the user enters

Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment Amount
---------------- ------------- ---- ---------------- ------------------- ----------------------
\$1375.0 1.175 7 Compound Monthly \$9.45221 \$1384.45
• March 13th, 2013, 06:46 PM
Norm
Re: Java Method Help
What happens now when you compile and execute the code?
Copy the output and paste it here and explain what is wrong with it.
• March 13th, 2013, 06:49 PM
07.350
Re: Java Method Help
It won't compile it gives me an error at
Code java:

`System.out.println("+numb1", "+numb2", "numb3", "+word1", "+numb4", "+numb5");`
saying the method println(string) in the type printstream is not applicable for the arguments ( string, string, string, string, string)
• March 13th, 2013, 06:56 PM
Norm
Re: Java Method Help
Quote:

the method println(string)
That says the println() method takes ONE String argument. The posted code has several String arguments separated by commas. Try removing the commas and making the arg one String by using the + operator to concatenate all the Strings into one String.
• March 13th, 2013, 07:08 PM
07.350
Re: Java Method Help
I changed it to
Code java:

``` /** display interest **/ public static void displayInterest(double numb1, double numb2, double numb3, char word1, double numb4, double numb5 ) { numb5 = numb1 + numb4;   System.out.println("Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment"); System.out.println("---------------- ------------- ---- ---------------- ------------------- ---------------"); System.out.println("+numb1 +numb2 +numb3 +word1 +numb4 +numb5"); }```

Then when I put it to where I want to print out when they select 1,2, or 3 it has an error
Code java:

``` switch(type) { case 1: displayInterest(principle,interest,term,"Simple",principle); break;   case 2: displayInterest(principle,interest,term,"Monthly Compounded",principle); break;   case 3:displayInterest(principle,interest,term,"Daily Compounded",principle); break;   default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type); break;   }```

It has an error saying it's arguments are not applicable
• March 13th, 2013, 07:13 PM
Norm
Re: Java Method Help
Please copy the full text of the error messages and paste it here. There is useful and important info in the messages.
• March 13th, 2013, 07:16 PM
07.350
Re: Java Method Help
Sorry, it won't compile but the error is the method displayInterest(double,double, double, char,double) in the type InterestCalculator4 is not applicable for the arguments (double, double,double, string, double)
• March 13th, 2013, 07:18 PM
Norm
Re: Java Method Help
Quote:

it won't compile
The compiler prints out error messages when it finds errors.
Here is a sample error message from the javac compiler:
Code :

```TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^```
It gives the source line number, the line with the error and describes the error.

Quote:

the method displayInterest(double,double, double, char,double) in the type InterestCalculator4
is not applicable for the arguments (double, double,double, string, double)
Did you compare those two argument lists and see where they are different? You need to change one of them so that they match.
• March 13th, 2013, 07:21 PM
07.350
Re: Java Method Help
Code java:

``` Exception in thread "main" java.lang.Error: Unresolved compilation problems: a cannot be resolved to a variable Monthly cannot be resolved to a variable Syntax error on token "Compounded", delete this token The method displayInterest(double, double, double, char, double, double) in the type InterestCalculator4 is not applicable for the arguments (double, double, double, String, double)```

at InterestCalculator4.main(InterestCalculator4.java: 45)
• March 13th, 2013, 07:26 PM
Norm
Re: Java Method Help
Quote:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
You should compile the code and fix the problems before trying to execute it. That error message is from when you try to execute the code. It won't execute with compiler errors!!!

Is this the error: Monthly cannot be resolved to a variable
Does the code use a variable named Monthly that is not defined?

See the end of post#10 for the other part.
• March 13th, 2013, 07:39 PM
07.350
Re: Java Method Help
Would I define it was a char or string? That's what I'm confused about
• March 13th, 2013, 07:41 PM
Norm
Re: Java Method Help
A char is one letter, a String allows for many characters. Which do you need? A String can be a single letter.
A char can never be more than one letter.
• March 13th, 2013, 07:50 PM
07.350
Re: Java Method Help
When I type in String it doesn't recognize it as a string variable.
• March 13th, 2013, 07:51 PM
Norm
Re: Java Method Help
Quote:

it doesn't recognize it as a string variable.
Please copy the full text of the error message and paste it here.
• March 13th, 2013, 08:02 PM
07.350
Re: Java Method Help
I got it to work! Thanks for your help and baring with me!
• March 13th, 2013, 08:05 PM
Norm
Re: Java Method Help
Glad you got it to work.