• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Newbie Question : cannot resolve symbol : variable

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ernest Friedman-Hill,

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

Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic