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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem with Java code for Project

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by CodyReuille; October 18th, 2011 at 04:03 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java code for Project

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

    Ashley

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Java code for Project

    I'm glad that i could help you, as I am just a beginner too.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

Similar Threads

  1. reformatting java code problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 23rd, 2010, 02:44 PM
  2. Java Web Browser Term Project- I am facing multiple problem...pls help
    By tazbinur in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 8th, 2010, 10:05 AM
  3. Replies: 2
    Last Post: August 1st, 2010, 07:29 AM
  4. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  5. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM