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 5 of 5

Thread: Having trouble with my program, can someone please help?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Having trouble with my program, can someone please help?

    //Imports the scanner utility
    import java.util.Scanner;

    public class ComputeLoan1
    {

    //Calls the method for variables to be calculated
    public static double calculations(double loanAmount, double monthlyInterestRate, int numOfYears)
    {

    //Declares calculations as monthlyPayment
    double monthlyPayment = (loanAmount * monthlyInterestRate / (1
    - 1 / Math.pow(1 + monthlyInterestRate, numOfYears * 12)));

    //Value that is returned to the main method
    return monthlyPayment;

    }//Closes the calculations method

    //Final method used to output users calculations
    public static void finalMethod(String[] args)
    {

    //Displays results
    System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) /100.0);

    }//Closes the finalMethod


    //Main method
    public static void main(String[] args) {


    //Declarations
    double loanAmount = 0;
    double annualInterestRate = 0;
    double monthlyInterestRate = 0;
    int numOfYears = 0;
    double calculations;

    //Creates a scanner for input from keyboard
    Scanner input = new Scanner(System.in);

    //Greeting to the user
    System.out.println("Welcome to Kentucky Credit Union Loan Calculator!");
    System.out.println("This program is used to help calculate the monthly loan amount.");
    System.out.println("");


    //Prompts the user for input
    System.out.print("Please enter the loan amount: $");
    loanAmount = input.nextDouble(); //Accepts input from user and stores it in a variable

    //Enter yearly interest rate
    System.out.print("Enter yearly interest rate, for example 7.25: ");
    annualInterestRate = input.nextDouble();

    //Obtain monthly interest rate
    monthlyInterestRate = annualInterestRate / 1200;


    //Prompts the user for input
    System.out.print("Please enter length of the loan in years as an integer, for example 5: ");
    numOfYears = input.nextInt();

    } //Closes the main method


    }//Closes the class


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble with my program, can someone please help?

    Please correctly format your code by using highlight tags:

    [highlight=Java]//your code goes here[/highlight]

    What exactly is your program? Does your code compile, or are there errors (if so, post them)?

    monthlyInterestRate = annualInterestRate / 1200;
    I haven't dealt with loan formulas very much, but this strikes me as odd. Wouldn't you want to divide the annual rate by just 12 to get the monthly rate?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with my program, can someone please help?

    This is the error I get:

    Documents\JCreator LE\MyProjects\ComputeLoan1\src\ComputeLoan1.java:2 5: cannot find symbol
    symbol : variable monthlyPayment
    location: class ComputeLoan1
    System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) /100.0);
    ^
    1 error

    the monthlyInterestRate = annualInterestRate / 1200; is correct. I used to know why you do it this way, but I forgot. It's how it is in my java programming book too.

    this is what the program is suppose to do:

    Your application will calculate the monthly payments due on a loan. The user will input the loan amount, the interest rate, and the loan's term (time to repay).

    You will accept input in the main method. You will use a separate method (other than main) to calculate the payment. You will use a third method to output the result. The third method (output) can be called from either the main method or the calculation method. Here is the formula you will use to calculate the payment:

    Payment Formula

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Having trouble with my program, can someone please help?

    Please format your code! :O

    monthlyPayment is declared in your calculations() method in your ComputeLoan1 class; you can't use it anywhere else in that class. The scope of a variable is when and where you can use it; scope is limited to the block of code in which you define a variable. This means that since you defined monthlyPayment in your calculations() method, you can ONLY use it within that method.

    However, the calculations() method returns monthlyPayment's value, so why not just call calculations()?

    Also, note that your method:

    public static void finalMethod(String[] args)

    Does not have to have args as an argument; only the main method must have a String[] as an argument. Your final method doesn't require any arguments, so you need not list any in its declaration.
    Last edited by snowguy13; February 18th, 2012 at 01:22 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. The Following User Says Thank You to snowguy13 For This Useful Post:

    cbutcher0007 (February 21st, 2012)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with my program, can someone please help?

    Ty very much for your help. I got it figured out. Ty

Similar Threads

  1. Trouble sending an email from my program
    By java99 in forum Java Networking
    Replies: 5
    Last Post: January 11th, 2012, 11:22 PM
  2. Trouble sending an email from my program
    By java99 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 11th, 2012, 05:27 PM
  3. Trouble with Java payroll program
    By Shawn Wray in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 4th, 2012, 12:57 AM
  4. [SOLVED] having a little trouble
    By XSmoky in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 6th, 2011, 08:48 PM
  5. Trouble with law of Cosine program
    By Delstateprogramer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 06:07 PM