I am writting a code to calculate the total payments of a car with interest. Also calculating the monthly payments. I keep getting errors, saying that i am not declaring price and rate...how do you declare them to some number?

this is my program.

public static void main(String[] args) 
    	{//start main method
    	
    	double price;//price of the car.
    	double rate; //the rate of interest in decimal form.
    	double total; //the total paid over the course of two years.
    	double mpayments; //monthly payments.
    	price=double num;
    	rate=double dnum;
    	
    	total = (num) * (dnum) * 2;//to calculate the total price
    	mpayments = (total) / 24;//to calculate the monthly payments
    	
    	
    
    	Scanner keyboard = new Scanner(System.in); 
    		
    	System.out.println("Enter the Price of the Car");
    	price=keyboard.nextDouble();
    	System.out.println(num);
    	
    	System.out.println("Enter Rate of Interest (ex: 4% is 0.04)");
    	rate=keyboard.nextDouble();
    	System.out.println(dnum);
    	
    	DecimalFormat numf = new DecimalFormat("$#######.00");
    	System.out.println("Your Total Payments Over 2 Years is" + numf.format(total) + ".");
    	System.out.println("Your Monthly Payments are " + numf.format(mpayments) + ".");
    	
    }//end main method
double price=0.0;
double rate=0.0; 
// other initializations

These are local (method) variables, which are also known as auto variables. And the tricky part is that auto variables are never initialized automatically for you as their name would rather suggest. You need to initialize them before using them, else the compiler complains as you've seen. If these would have been instance/class variables, java would have initialized them to a default for you. For example an int variable from a class is initialized by default to 0, a double to 0, an object reference to null and so on.

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.