Problem with Java code for Project
Hi everyone, I'm new here and new to Java (I guess this is how all newbies start their threads :P)
Anyway, I have to make a program that will calculate present value. We have to do the calculations in a separate function and my code will nonot run properly.
The formula for this is P = F/ (1 + R)^n
Where P = the present value they must invest now. F is the future value of what they want their investment to accumulate to, and N is the number of years they plan to keep it invested.
Any help would be greatly appreciated.
Code :
import java.util.Scanner;
public class project2
{
public static void main(String[] args)
{
System.out.println("This program will determine present value.");
displayValue(P);
System.out.println("You will need to invest " + P + " to reach your desired future value.");
}
public static void displayValue(double P)
{
double rate;
double future;
double years;
double P;
//create a scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the annual interest rate: ");
rate = keyboard.nextDouble();
system.out.print("Please enter what you want the future value to be: ");
future = keyboard.nextDouble();
System.out.print("Please enter the amount of years you will keep money in account: ");
years = keyboard.nextDouble();
firstExp = (1 + rate);
secondValue = Math.pow(firstExp,years);
P = future/secondValue;
}
}
Re: Problem with Java code for Project
There are many problems, but lets start out with your variables.
displayValue(P); does not exist because you haven't declared what it is, even tho you did double P; it dosnt work because you have to initialize the variable before you can use it.
for example double p = whatever;
you also need to declare all the var's down at the bottom like secondValue, firstExp etc
another think is on
system.out.print("Please enter what you want the future value to be: ");
the s in System needs to be uppercase
something like this, it has bad organization but it works, you also have to set all the vars to whatever they are supposed to be
Code Java:
import java.util.Scanner;
public class testing {
public static void main(String[] args) {
double P = 0;
System.out.println("This program will determine present value.");
displayValue(P);
System.out.println("You will need to invest " + P
+ " to reach your desired future value.");
}
public static void displayValue(double P) {
double rate;
double future;
double years;
// create a scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the annual interest rate: ");
rate = keyboard.nextDouble();
System.out.print("Please enter what you want the future value to be: ");
future = keyboard.nextDouble();
System.out
.print("Please enter the amount of years you will keep money in account: ");
years = keyboard.nextDouble();
double firstExp = (1 + rate);
double secondValue = Math.pow(firstExp, years);
P = future / secondValue;
}
}
its not exactly what you want, but it at least shows you what you need to do
Re: Problem with Java code for Project
Hey, Cody! Thanks a lot for your help. It's good to know that I'm only making simple mistakes thus far. I get so frustrated sometimes that I want to give up. I've been working on this for the last week and I've been so worried that I won't get it turned in, in time. I feel much better now.
I have a question that maybe you can answer. Is it like C++ where I need to create a prototype at the top before the main function for my "displayValue()" ? My program finally runs for the first time ever, but I get 0.0 for the answer of present value.
Thank you again!
Ashley
Re: Problem with Java code for Project
No, you don't need to have a function declaration before you define it. (In fact, doing so will confuse the compiler I think.)
Re: Problem with Java code for Project
Thank you, Penguin! I'll keep working at it then.
Ashley
Re: Problem with Java code for Project
I think I got it! I moved the output statement from the main function back to the displayValue function and I get a real number instead of 0.0!
Ashley
Re: Problem with Java code for Project
I'm glad that i could help you, as I am just a beginner too.
Re: Problem with Java code for Project
As soon as the thread gets solved, kindly mark it SOLVED.
And always use Edit post if you want to post something. Avoid multiple posts. Thanks and Good luck.