0

I'm trying to figure out why "monthlyPayment" cannot be found when it is within the parameter of the method I called, which is "interest_Total" (I marked it with two asterisks next to it). Below is my code, followed by the error.

import java.util.*;
public class TestCC
{
   public static void main(String[] args)
  {
  Scanner kb = new Scanner(System.in);
  
  System.out.println("Welcome to the payment calculator");
  System.out.println("List of options");
  System.out.println("MP: To calculate the Monthly Payment for a fix-rate, fix-term loan");
  System.out.println("M: To calculat ethe number of Months to pay off a loan witha  fixed monthly payment");
  
  System.out.print("Please enter MP or M: ");
  String choice = kb.next();
  
  while (!(choice.equals("MP") || choice.equals("M")))
  {
     System.out.print("Error; Enter MP or M: ");
  }
  
  if (choice.equals("MP")) 
  {
     // Loan Amount
     
     System.out.print("Enter loan amount: ");
     while (!kb.hasNextDouble())
     {
        kb.next();
        System.out.print("Enter loan amount: ");
     }
     double loan = 0;
     loan = kb.nextDouble();
     
     
     // Term Amount
     
     System.out.print("Enter term in years: ");
     while (!kb.hasNextInt())
     {
        kb.next();
        System.out.print("Enter term in years: ");
     }
     int years = 0;
     years = kb.nextInt();
     
     
     // Interest Rate
     
     System.out.print("Enter the interest rate: ");
     while (!kb.hasNextDouble())
     {
        kb.next();
        System.out.print("Enter the interest rate: ");
     }
     double interestRate;
     interestRate = kb.nextDouble(); 
              
     // Calling methods Part 1
     payment(loan, years, interestRate);
     
     **interest_Total(monthlyPayment, years, loan);**
       
  }
 }
public static double payment(double loan, int years, double interestRate)

  {
  double monthlyPayment = 0;
        
  monthlyPayment = (loan * (interestRate/12))/(1 - (1/(Math.pow((1 + (interestRate/12)),(years * 12)))));
  System.out.printf("Monthly Payment: $%.2f", monthlyPayment);
  
  return monthlyPayment;                                    
  }

public static double interest_Total(double monthlyPayment, int years, double loan)

  {
  double totalInterest2 = 0;
  totalInterest2 = ((monthlyPayment * years * 12) - loan);
  System.out.printf("Total Interest Paid: %.2f", totalInterest2);
  
  return totalInterest2;
 }
}

Below is the error I get

TestCC.java:63: error: cannot find symbol
     interest_Total(monthlyPayment, years, loan);
                    ^
 symbol:   variable monthlyPayment
 location: class TestCC
 1 error
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Yahya
  • 13
  • 2
  • I realized I added the comment twice, sorry – Yahya Jun 20 '20 at 06:10
  • When you call a function, you call it using values that make sense *in the context where you're calling it*, not using the function's names for things. The point is to tell Java what value to take *from* the main method and put *into* what `interest_Total` will refer to as `monthlyPayment`. – Karl Knechtel Jun 20 '20 at 06:16

5 Answers5

0

monthlyPayment is the name of a parameter of the interest_Total function. monthlyPayment is a variable in the payment function. But there is nothing by that name in main where you are calling that function from. Perhaps this is what you meant?

double monthlyPayment = payment(loan, years, interestRate);
interest_Total(monthlyPayment, years, loan);
Delta_G
  • 2,927
  • 2
  • 9
  • 15
  • I understand the reason why now thank you. I didn't know I had to declare the variable again in the main method. I though since I had it as a return statement I can just use it within a parameter of a different method. – Yahya Jun 20 '20 at 06:40
  • The return statement just returns the value, it doesn't create a new variable for you or anything. It just gives you back the value. – Delta_G Jun 20 '20 at 14:16
0

Change this line

payment(loan, years, interestRate);

To this

double monthlyPayment = payment(loan, years, interestRate);

DSA User
  • 11
  • 3
0

The scope of variable "monthlyPayment" is within the body of the "payment" fuction. In order to use that variable and pass it to "interest_Total", you can do this:

double monthlyPayment = payment(loan, years, interestRate);
interest_Total(monthlyPayment, years, loan);

You can learn more about the scope of variables in java here

Usama
  • 411
  • 4
  • 6
0

You can't share variables between methods, since monthlyPayment is defined in payment it is local to that method. If you want to access the variable in another method you need to specify them as member variables in the class.

7pixels
  • 51
  • 4
0

You are getting this error because you haven't declared the monthlyPayment variable before using it to pass a function. Try this:

    double monthlyPayment = payment(loans, years, interestRate);
Nishith Savla
  • 310
  • 2
  • 10