Hi guys. I am getting the error: LoanProgram.java:17: variable payment might not have been initialized
payment = getPayment (amount, rate, years, months, payment);
I am stumped, and the lab is due tomorrow. If anyone could help me out I would greatly appreciate it. :)
I'll put an asterisk on the line with the error.

import java.util.Scanner;
	import java.text.DecimalFormat;
	
	 public class LoanProgram
	 {
	 
	 	static Scanner input = new Scanner (System.in);
		 public static void main (String [] args)
		 {
		 	int years, months;
			double amount, rate, payment;
			amount = getAmount ();
			rate = getRate ();
			years = getYears ();
			months = years * 12;
			*payment = getPayment (amount, rate, years, months, payment);
			System.out.println (payment);
		 }
		 
		 public static double getAmount ()
		 {
		 	System.out.print ("Enter the amount you are borrowing: ");
			double amount = input.nextDouble ();
			if (amount < 0 || amount > 100000)
			{
				System.out.print ("invalid. Enter amount: ");
				amount = input.nextDouble ();
			}
			return amount;
		 }
		 
		 public static double getRate ()
		 {
		 	System.out.print ("Enter the annual interest rate as a percent: ");
			double rate = input.nextDouble ();
			if (rate < 0)
			{
				System.out.print ("You can't have a negative rate. Enter amount: ");
				rate = input.nextDouble ();
			}
			return rate;
		  }
		  
		  public static int getYears ()
		  {
		  	System.out.print ("Enter the length of the loan in years: ");
			int years = input.nextInt ();
			if (years < 1)
			{
				System.out.print ("The minimum amount of years is 1. Enter amount: ");
				years = input.nextInt ();
			}
			return years;
			}
			
			public static double getPayment (double amount, double rate, int years, int months, double payment)
			{
				months = years * 12;
				payment = amount * rate * (Math.pow(rate + 1, months)/((Math.pow(rate + 1, months)-1)));
				return payment;
			}
}

Recommended Answers

All 8 Replies

You pass the variable "payment" as the last parameter to that method, but you have never initialised(given a value to) payment before making the call.
Given tha the method is supposed to calculate payment, I can't see why you need to pass it as a parameter at all.

When I take payment out of the parameter, it gives the error: LoanProgram.java:61: cannot find symbol
That's why I thought I needed it in the parameter, since I have to call it later on.
I'm clueless >-<

You just need to declare it as a local variable inside the method.

Don't I declare it in the main method, by assigning it to the getPayment method?
Well I'm assuming the answer is 'no' O.o

Just declare one in the method. You can use that for calculations and a return value, and java will forget it after the method is finished. That's called a local variable.

You didn't declared it. just declare it in the main method

You didn't declared it. just declare it in the main method

No no no. You haven't followed the whole conversation here.
The program needs one variable in the main method PLUS a local variable in the getPayment method. The original problem came from trying to pass the main method's var in as a parameter rather than creating a local var in getPayment.

commented: very helpful +0
Be a part of the DaniWeb community

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