Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie Question : cannot resolve symbol : variable

 
Michael Lukatchik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I've been working on an introductory Java program and encountered the following error:

cannot resolve symbol
symbol : variable interestRate
location: class FutureValueApp
futureValue = (futureValue + monthlyPayment) * (interestRate + 1);
The compiler does not like the interestRate variable in this statement.

In my research, I verified that my environment variables pointed to my \bin directory and that I configured the CLASSPATH variable, which was set with a value of a period (.)

I'm posting the code that I've worked with so far. If you can spot the error here, or if you place it into your editor and run it, please give me some feedback as to how I can troubleshoot this problem. Thanks in advance!
Mike

---------------------------------------------------------

import javax.swing.*;
import java.text.*;

public class FutureValueApp
{
public static void main(String[] args)
{
String choice = "";

while (!(choice.equalsIgnoreCase("X")))
{
String paymentString = JOptionPane.showInputDialog
("Enter monthly payment: ");
double monthlyPayment = Double.parseDouble(paymentString);

String rateString = JOptionPane.showInputDialog
("Enter yearly interest rate: ");
double interestRate = Double.parseDouble(rateString);
double monthlyInterestRate = interestRate/12/100;

String yearString = JOptionPane.showInputDialog
("Enter number of years: ");
int years = Integer.parseInt(yearString);
int months = years * 12;

//Call the static method that contains the input of these three fields
double futureValue = calculateFutureValue(monthlyPayment, monthlyInterestRate, months);

NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(2);

String message =

"Monthly payment: " + currency.format(monthlyPayment) + "\n"
+ "Yearly interest rate: " + percent.format(interestRate/100) + "\n"
+ "Number of years: " + years + "\n"
+ "Future value: " + currency.format(futureValue) + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'X'";

choice = JOptionPane.showInputDialog(null, message, "Future Value", JOptionPane.PLAIN_MESSAGE);
}
System.exit(0);
}

private static double calculateFutureValue(double monthlyPayment, double monthlyInterestRate, int months)
{
int i = 1;
double futureValue = 0;

while (i <= months)
{
futureValue = (futureValue + monthlyPayment) * (interestRate + 1);
i++;
}
return futureValue;
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,

Welcome to JavaRanch!

The variable "interestRate" is declared in main(); it's not visible inside other methods. You can pass it as a parameter to calculateFutureValue(), the same way you've passed the other parameters.
 
Michael Lukatchik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ernest Friedman-Hill,

I see, it was all about the scope of the variable...Thanks a ton!

Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic