Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Problem with Java code for Project

1. ## 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 )

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.

```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;

}
}```

2. ## 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
```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

3. ## 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

4. ## 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.)

5. ## Re: Problem with Java code for Project

Thank you, Penguin! I'll keep working at it then.

Ashley

6. ## 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