Okay, I am a new student (to Java--well programming in general) and the first assignment we have is to create a program that computes the future value of an investment based on three user inputs (principle deposit, interest rate, and length of term in years).

First of all, I am at a loss as how to assign variable that are not constant. Principle, rate, and term are set to whatever the user decides to input when prompted. But that is the least of my problems. I just keep getting error code after error code when building the file.

I am not asking anyone to tell me what the solution to my problems are, as I want to do my own work... but I would like help with walking me through the process of debugging the program.. maybe through examples that are similar or something. I am aware that programming is the easy part, its the debugging that is challenging.

Here is my program...

``````/**
* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = scan.nextDouble();

System.out.println("What is the annual interest rate? ");
double rate = scan.nextDouble();

System.out.println("What is the term of the investment in years? ");
int years = input.nextInt();
}

public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double calcFV(double p, double r, int t)
{
double value1 = p; // assign the variable p to principle
double value2 = r; // assign the variable r to rate
int value3 = t; // assign the variable t to years

int calcFV(double p, double r, int t)
{
int fv = p * Math.pow( (1.0 + r/100), t);
return fv;
}

System.out.println("Your investment will be worth approximately: " + calcFV(p, r, t) );
}
} // end HW02_jsn``````

I very much appreciate any and all feedback!

Use code tags for posting your code, it just make the code more readable.

Some of the errors that I see here are :
1. You haven't declared what `scan` is. I guess …

Okay, after reading your post, you appear to me as someone really wanting to learn. Thats good. Keep Up.
Firstly, you can certainly post the instructions you have been given for the assignment it would only help us help you. The only thing this forum does not like is people …

> Where 'this' as mentioned above refers to the current object. Since
> static members do not belong to any particular object/instance as
> such, for them 'this' does not make any sense.

...but is still valid though is ignored. It is completely legal to invoke a static method …

Firstly, you haven't called the method `spalsh()` , though this won't in any way affect the working of your code, but just mentioning the fact. So you should include a statement such as this as your first statement in the main method.

``splash();``

Secondly, you say the program stops …

Look again at your variable declarations in main(). You'll see that you declared other variables for those values, but p, r, and t are not those variables.

## All 20 Replies

Use code tags for posting your code, it just make the code more readable.

Some of the errors that I see here are :
1. You haven't declared what `scan` is. I guess you are supposed to use `input` here but you have used `scan` .

``````public static double calcFV(double p, double r, int t)
{
double value1 = p; // assign the variable p to principle
double value2 = r; // assign the variable r to rate
int value3 = t; // assign the variable t to years

int calcFV(double p, double r, int t)
{
int fv = p * Math.pow( (1.0 + r/100), t);
return fv;
}``````

Here you have defined a method within another method having the same name but a diffrent signature. I am unable to gather what you are trying to achieve here.

Also I see all of you methods being static, can you explain the purpose of that ? I guess since you are calling them from `main` which is a static method, you have made all the other methods static too. Is it ?

Also post what errors you are getting.
Lastly since you say you are a begginner in java there is a wonderful sticky thread at the start of this forum, that would be helpful for you.

Thank you for the response. Sorry about not using code tags, I was a little hasty in posting...

Actually, not declaring scan hasn't caused any problems with my program at all... it stops running after that method. (Maybe I am missing something though... )

"Here you have defined a method within another method having the same name but a diffrent signature. I am unable to gather what you are trying to achieve here"
--Honestly, I didn't realize that (I am still trying to understand reading and writing code). And as far as understanding the purpose of the program... would it be easier if I post the instructions for the assignment? (Strictly to help you understand the purpose though.)

I have read and re-read the text, lecture notes, e-mailed my professor, done research on the internet, and I still don't understand what I'm doing... I think I am over analyzing things... I do that a lot!

Static, hmm... my professor told me that all the methods we would be using are static. It would probably be better for me to do some more research and find out why he would say that. But, I do know he want us to use calling for other methods, so possibly your assumption is right. It makes sense. (I am sorry for my lack of knowledge/understanding... I have only been to one class so far... we meet again on the 26th)

All my errors were on the same line (line 44:int calcFV(double p, double r, int t))
1. ';' expected
2. <identifier> expected
3. not a statement
4. ';' expected
5. ';' expected

And thank you for suggesting that posting, but I have already read it... and yes it did have some really good information. It also helped me find the Java book I have been wanting to buy... (I planned on buying a book for Java, but I wasn't sure which ones would be best for my experience level --- or lack-there-of).

Thanks again!!

I made some changes to my program... just trying to see if anything makes a difference (well, that and I read ahead in my text book and found some better ways to attempt what I am trying to do)

My slightly altered code is:

``````/**
* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
Scanner input = new Scanner(System.in);

public static void main(String[] args)
{
// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = scan.nextDouble();

System.out.println("What is the annual interest rate? ");
double rate = scan.nextDouble();

System.out.println("What is the term of the investment in years? ");
double years = scan.nextDouble();
}

public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double calcFV(double p, double r, double t)
{
double value1 = p; // assign the variable p to principle
double value2 = r; // assign the variable r to rate
double value3 = t; // assign the variable t to years

double calcFV = p * Math.pow( (1.0 + r/100), t);
return calcFV;

System.out.println("Your investment will be worth approximately: " + calcFV(p, r, t) );
}
} // end HW02_jsn``````

and these are the new errors...

Lines 22, 25, 28: (double profit/rate/years = scan.nextDouble();) cannot find symbol variable scan

So it seems as though you, verruckt24 were correct and i will try using input as you suggested.

All previously mentioned errors are gone.

Update:
As I mentioned I would change lines 22, 25, and 28 from a scan to an input.

When I did that, this new error was found for all three lines

non-static variable input cannot be referrence from a static context

I guess that is simple enough, self-explanatory. But either way it is written, there are still errors... I guess I will keep changing things around and see what happens.

commented: Its good to see you going ahead on your own rather than waiting for a push from anyone else +5

Okay, after reading your post, you appear to me as someone really wanting to learn. Thats good. Keep Up.
Firstly, you can certainly post the instructions you have been given for the assignment it would only help us help you. The only thing this forum does not like is people posting assignments instructions and asking members to code it for them, so as long as you are not doing that, it's fine.
Secondly, all the errors sited by you are because of the method declaration within the method, I think if you remove that most of your errors will certainly vanish.

Also about the 'static' methods part, I guess a little reading about the static keyword would be of much help. Where and when should the static keyword be used is of prime importance for it to be used.

Please ignore the above post as it is an intermediate version of this post, where I accidently clicked on the save button.

Yes, so as I said the `scan` was indeed creating problems weren't it ? It had to.

As far as this error goes "non-static variable input cannot be referrence from a static context"
If you declare the `input` variable in the `main` method I think it should clear up the errors. Also since you are not using this variable elsewhere, it should not cause problems.

``````public class HW02_jsn
{
Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
// code
}
}``````

You would need to do this

``````public class HW02_jsn
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// other code
}
}``````

To just explain you something about the 'static' keyword, 'static' means something that is for the entire class rather than for an instance. You might have heard or would hear member variables being called instance variables. This is because, for variables that aren't declared 'static' exists for each instance of the class. So, for e.g. a class Square might have `side` , which would specify the length of the side, as an instance variable, since each Square object might have different lenght of it's side, so each instance needs to have a `side` variable of it's own.
But Square, the class, might have a static `noOfSides` variable, which being static won't be existing independently for all Square instances, but just one for the entire class. And it so happens that this variable as you might figure out isn't required to be there for each instance either. Since irrelevant of the length of it's side, each Square would have just four sides. I hope you are able to get this.

Now towards the explanantion of your error. In Java there is a 'this' variable that stores a reference to the current object. So, when you write `calcFV()` without mentioning any class/object name before it, java assumes that you are referring to the same class' method/variable and changes the call to `this.calcFV()` . Where 'this' as mentioned above refers to the current object. Since static members do not belong to any particular object/instance as such, for them 'this' does not make any sense. So when you call a non-static member variable/method from a static method, the 'this' variable cannot be used for dereferencing as mentioned above the JVM has no way of knowing which class' method/variable you are referring to, and hence the compiler itself gives you an error mentioning the error statement.

Well this is my explanation, and I may as well be wrong (somehwere in the details). If you like an explanation from the Java guys themselves about this topic it can be found here

> Where 'this' as mentioned above refers to the current object. Since
> static members do not belong to any particular object/instance as
> such, for them 'this' does not make any sense.

...but is still valid though is ignored. It is completely legal to invoke a static method using a reference though misleading at best.

> So, when you write calcFV() without mentioning any class/object
> name before it, java assumes that you are referring to the same
> class' method/variable and changes the call to this.calcFV() .

Not always true since if the method by the name calcFV() is not present in the class hierarchy, invocation of a static method by the same name is attempted [in which case there is no `this'] or a compilation error with no such method exists is spit out.

> Now towards the explanantion of your error. In Java there is a
> 'this' variable that stores a reference to the current object.

Or put in a more detailed manner, a reference to the instance currently executing the given method [the method which is present on the top of the callstack of the currently executing Thread].

> So when you call a non-static member variable/method from a static
> method, the 'this' variable cannot be used for dereferencing as
> mentioned above the JVM has no way of knowing which class'
> method/variable you are referring to

This explanation is a bit misleading based on my above comments. Simply put, static entities of a class can exist independently without instances of the class floating around.

A small test program:

``````public class Test {

public static void main(final String args[]) throws Exception {
new Test().doInstance();

Test t = null;
t.doStatic(); // Expecting NPE? Nope, since `t' is never used.
}

private static void doStatic() throws Exception {
System.out.println("Do something *static* here...");
}

private void doInstance() throws Exception {
System.out.println("Do something *instance* related here...");
doStatic();
}

}
/* OUTPUT:
[java] Do something *instance* related here...
[java] Do something *static* here...
[java] Do something *static* here...
*/``````

> But Square, the class, might have a static noOfSides variable, which being
> static won't be existing independently for all Square instances, but just one
> for the entire class.

Or more accurately, one per class, per class loader, per VM, per application.

commented: I liked the: 'Expecting NPE' comment. Didn't know it was possible +5
commented: Got to know loads of things. +1

Wow, if not for the 'Location : India' I would have started believing that ~s.o.s~ woould be the username of Mr. James Gosling himself, that was so good. :)

Just to add a thing, yes static objects can be called by instance names too, I just meant that non-static members cannnot be called without the current object's reference.
Also, static members can certainly exists even without a single instance existing.

Wow.. thank you for the advice... I will see what changes I can make and update... as I am sure I will have some other changes to be made. That is fine though.

And yes I really do want to learn this... at first I wasn't so sure, but while I was attempting this code, the more problems I ran into, the more frustrated I got, but also the more determined I became to understand it. Usually I would have given up by now, but not this time!

Ok, so I will post the assignment instructions now, hopefully it will help you understand the point of the program.

Write a program to calculate the future value of an investment paying compound interest, based on user inputs for principal (p) deposited (the amount of the investment), the annual interest rate (r) paid (in percent), and term (t) of the investment (in years). Assume that interest is paid annually. The formula for computing future value (fv) is:

fv = p(1 + (r / 100))t

Write your class (program) with three methods:

A main method that obtains user inputs and displays the results
A splash method that displays a splash screen describing what the program does
A calcFV method that calculates and returns the future value, given three values as arguments by the calling method: principal, interest rate, and years

I will be posting an update version of my code shortly... and I do appreciate all the help! This site has helped me more than the text book for the class so far.

And I know it is strange that I am using all static methods, even though it does not make sense.... when I e-mailed my profressor (when first having problems) he specifically told me that the syntax of my methods should be:

``````public static void main(String args[])
{
// call splash()
// input principle, rate, years
// display calcFV(principle, rate, years)
}

public static void splash()
{
statements to display first four lines
}

public static double calcFV(double p, double r, double t)
{
statements to calculate fv
return fv;
}``````

He has made everything static... I have been reading ahead of the class to try to figure things out... but so far the text book hasn't even expained what static means, so I appreciate the explanations.

I updated my code (according to verruckt24's suggestions in post 6)

Here is the new code:

``````/**
* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

public class HW02_jsn
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = input.nextDouble();

System.out.println("What is the annual interest rate? ");
double rate = input.nextDouble();

System.out.println("What is the term of the investment in years? ");
double years = input.nextDouble();

System.out.println("Your investment will be worth approximately: " + calcFV(p, r, t) );
}

public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double calcFV(double p, double r, double t)
{
double value1 = p; // assign the variable p to principle
double value2 = r; // assign the variable r to rate
double value3 = t; // assign the variable t to years

double Math.calcFV = p * Math.pow( (1.0 + r/100), t);
return calcFV;
}
} // end HW02_jsn``````

I am only getting one error when I build the program now. It is on line 45 (double Math.calcFV = p * Math.pow( (1.0 + r/100), t);) - ';' expected. What does that mean? I already have a ; on that line...

and just to verify, when mentioning class: the this.calcFV thing... Did I change it correctly by chainging it to Math.calcFv etc...?

What exactly were you expecting when you declared:

``double Math.calcFV = p * Math.pow( (1.0 + r/100), t);``

Since a '.' isn't a valid character when used to name variables, the compiler sees this as:

``double Math(something-unexpected-when-it-should-have-been-a-semicolon-to-end-the-declaration)``

Hence the given message.

``````double Math.calcFV = p * Math.pow( (1.0 + r/100), t);
return calcFV;``````

Do it this way

``````double futureValue = p * Math.pow( (1.0 + r/100), t);
return futureValue;``````

Alright, I have been reading the information on the Java tutorials and between that and the information I am getting from you guys I think I am beginning to understand more... although it is a long process. I have adjusted my program (again ever so slightly)...

``````* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
public int principle;
public int rate;
public int years;

public void main(String[] args)
{
Scanner input = new Scanner(System.in);

// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = input.nextDouble();
System.out.println("What is the annual interest rate? ");
double rate = input.nextDouble();
System.out.println("What is the term of the investment in years? ");
double years = input.nextDouble();

System.out.println("Your investment will be worth approximately: " + futureValue);
}
public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double futureValue(double p, double r, int t)
{
double futureValue = p * Math.pow( (1.0 + r/100), t);
return futureValue;
}
} // end HW02_jsn``````

One error was found on line 32 (System.out.println("Your investment will be worth approximately: " + futureValue);) stating that cannot find symbol variable futureValue.

I am assuming that this error is because I haven't declared futureValue.

I am sorry if I seem bothersome... it is really aggrevating knowing that I haven't gotten this yet.

Keep the method name as it is. I was just suggesting change in the variable name.

What I mean here is

``````public static double calcFV(double p, double r, double t)
{
double value1 = p; // assign the variable p to principle
double value2 = r; // assign the variable r to rate
double value3 = t; // assign the variable t to years

double futureValue = p * Math.pow( (1.0 + r/100), t);
return futureValue;
}``````

Also keep the call to the method in the println statement unchanged

``System.out.println("Your investment will be worth approximately: " + calcFv(p,r,t));``

I think you have got yourself confused with method and variables. As ~s.o.s~ has mentioned read some java tutorials like this one and practice some more examples to get things sorted out.

Ok... so with some more tinkering, I finally got the program to complete the process when I built it... However, when I ran the program, this is what I got:

java.lang.NoSuchMethodError: main
Process completed.

UGH!

So here is the program once again:

``````/* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
public int principle;
public int rate;
public int years;

public void main(String[] args)
{
Scanner input = new Scanner(System.in);

// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = input.nextDouble();
System.out.println("What is the annual interest rate? ");
double rate = input.nextDouble();
System.out.println("What is the term of the investment in years? ");
double years = input.nextDouble();
}
public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double calcFV(double p, double r, int t)
{
double futureValue = p * Math.pow( (1.0 + r/100), t);

System.out.println("Your investment will be worth approximately: " + futureValue);
return futureValue;
}
} // end HW02_jsn``````

THANKS AGAIN!

ok forget my previous post. I got the program to compile and run... no errors this time. BUT... and this is a big one... its not calculating the futureValue. It stops running after the main method (where it asks for user inputs) and after the user answers those three questions (about principle, rate, and term) the process completes. What is going on??? Why won't it compute?

Here is the code

``````/ * HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
public int principle;
public int rate;
public int years;

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

// call splash()
// input principle, rate, years
// display calcFV (principle, rate, years)

System.out.println("What was the amount of the principle deposit?  ");
double profit = input.nextDouble();
System.out.println("What is the annual interest rate? ");
double rate = input.nextDouble();
System.out.println("What is the term of the investment in years? ");
double years = input.nextDouble();
}
public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}
public static double calcFV(double p, double r, int t)
{
double futureValue = p * Math.pow( (1.0 + r/100), t);

System.out.println("Your investment will be worth approximately: " + futureValue);
return futureValue;
}
} // end HW02_jsn``````

Firstly, you haven't called the method `spalsh()` , though this won't in any way affect the working of your code, but just mentioning the fact. So you should include a statement such as this as your first statement in the main method.

``splash();``

Secondly, you say the program stops after requesting the three inputs from the user, but haven't you written it that way? You haven't made a call to the calcFV method after asking for the input. So do that.

Also since you are printing out the `futureValue` in the `calcFV` method itself, you can opt out returning it with the return statement but if you absolutely want to, you will also have to accept it in the statement that calls the method.

So after making the changes I have suggested you. your program should have a form like this.

``````import java.util.Scanner;

public class HW02_jsn{
public static void main(String [] args){
// the three input requesting statements, with the user input scanning stmts in between them.
double futureValue = calcFV(p,r,t); // this futureValue is different than the one declared inside the method calcFV
System.out.println("Value is " + futureValue);
// splash code here
// calcFV code here
} // end of main
}// end of class``````
commented: You have been very helpful and patient with me. I appreciate that very much! +1

Thank you for the response again. I realize that somehow I did write the code to ignore the calculation... but I did not mean to, nor did I realize.

I re-wrote the code according to your suggestions:

``````/**
* HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
public int principle;
public int rate;
public int years;
public int futureValue;

public static void main(String[] args)
{
splash();

Scanner input = new Scanner(System.in);

System.out.println("What was the amount of the principle deposit?  ");
double profit = input.nextDouble();
System.out.println("What is the annual interest rate? ");
double rate = input.nextDouble();
System.out.println("What is the term of the investment in years? ");
double years = input.nextDouble();

double futureValue = calcFV(p, r, t);
System.out.println("Your investment will be worth approximately: " + futureValue);
}
public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}

public static double calcFV(double p, double r, double t)
{
// p = double principle
// r = double rate
// t = double years

double futureValue = p * Math.pow( (1.0 + r/100), t);
return futureValue;
}

// main()
} // end HW02_jsn``````

Now I am only getting 3 errors, that are the same error in their own.

Line 31 (double futureValue = calcFV(p, r, t);)
1. cannot find symbol variable p
2. cannot find symbol variable r
3. cannot find symbol variable t

I though I declared the variables... but apparently I went wrong somewhere.

Look again at your variable declarations in main(). You'll see that you declared other variables for those values, but p, r, and t are not those variables.

commented: Thank you... you caught the obvious that I was missing all along! +1

Oh my god!!!! SOOOO excited!!! It worked! Thank you soooooooo much!

Just so you can see the final product, here it is:

``````/*HW02_jsn.java
* Jacqualyn Nelson
* 2009/1/16
*
* Calculate the future value of an investment paying compound interest annually
*/

import java.util.Scanner;

public class HW02_jsn
{
public int principle;
public int rate;
public int years;
public int futureValue;

public static void main(String[] args)
{
splash();

Scanner input = new Scanner(System.in);

System.out.println("What was the amount of the principle deposit?  ");
double p = input.nextDouble();
System.out.println("What is the annual interest rate? ");
double r = input.nextDouble();
System.out.println("What is the term of the investment in years? ");
double t = input.nextDouble();

double futureValue = calcFV(p, r, t);
System.out.println("Your investment will be worth approximately: " + futureValue);
}
public static void splash()
{
System.out.println("This program calculates an approximation of the future value of an investment based on these three variables:");
System.out.println("The amount of the principle deposit");
System.out.println("The amount of the annual interest rate");
System.out.println("The term of the investment in years");
}

public static double calcFV(double p, double r, double t)
{
// p = double principle
// r = double rate
// t = double years

double futureValue = p * Math.pow( (1.0 + r/100), t);
return futureValue;
}

// main()
} // end HW02_jsn``````

Again, THANK YOU ALL FOR THE HELP!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.