Java help

P!rate

Move Like a MoFo
Joined
Mar 16, 2009
Messages
6,073
I have a lab for java, i feel like this is an easy mistake that i'm overlooking, but plz help, the point is to mak e aprogram that calculates mortage

import java.util.Scanner;
import java.text.DecimalFormat;

public class LoanProgram
{

Scanner console = new Scanner(System.in);
public static void main (String [] args)
{
int years;
double amount, rate, payment;
amount= getAmount();
rate = getRate();
years = getYears();
payment = findPayment (amount, rate, years);
displayPayment (payment);
}
public static double getAmount()
{
System.out.print ("Please enter the amount to be borrowed: ");
int a = console.nextDouble();
if (a < 0 || a > 100000)
{
System.out.print ("Invalid. Enter Amount: ");
a = console.nextDouble();
}
return a;
}
public static double getRate()
{
System.out.print("Please enter the annual interest rate as a percent: ");
int b = console.nextDouble();
if (b < 0 || b >100)
{
System.out.print ("Invalid. Number must be between 0 and 100. Enter again: ");
b = console.nextDouble();
}
return b;
}
public static double getYears()
{
System.out.print("Enter the length of the loan in years: ");
int c = console.nextDouble();
if (c < 0 || c > 100)
{
System.out.print ("Error. Please re-enter the years: ");
c = console.nextDouble();
}
return c;
}
public static double findPayment (double a, double b, double c)
{
return a*b*((Math.pow (b+1, c/12))/((Math.pow (b+1, c/12))-1));
}
public static double displayPayment (double x)
{
System.out.println(x);
}
}

sorry for my poor style, these are the errors

LoanProgram.java:25: possible loss of precision
found : double
required: int
years = getYears();
^
LoanProgram.java:32: non-static variable console cannot be referenced from a static context
int a = console.nextDouble();
^
LoanProgram.java:32: possible loss of precision
found : double
required: int
int a = console.nextDouble();
^
LoanProgram.java:36: non-static variable console cannot be referenced from a static context
a = console.nextDouble();
^
LoanProgram.java:36: possible loss of precision
found : double
required: int
a = console.nextDouble();
^
LoanProgram.java:43: non-static variable console cannot be referenced from a static context
int b = console.nextDouble();
^
LoanProgram.java:43: possible loss of precision
found : double
required: int
int b = console.nextDouble();
^
LoanProgram.java:47: non-static variable console cannot be referenced from a static context
b = console.nextDouble();
^
LoanProgram.java:47: possible loss of precision
found : double
required: int
b = console.nextDouble();
^
LoanProgram.java:54: non-static variable console cannot be referenced from a static context
int c = console.nextDouble();
^
LoanProgram.java:54: possible loss of precision
found : double
required: int
int c = console.nextDouble();
^
LoanProgram.java:58: non-static variable console cannot be referenced from a static context
c = console.nextDouble();
^
LoanProgram.java:58: possible loss of precision
found : double
required: int
c = console.nextDouble();
^
13 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
 
I am a little curious why you are getting a double with console.nextDouble() and storing it as an int. Either cast it to an int, or store it as a double. You could also use console.nextInt() to return an int. The precision errors are caused because you are mixing types going from more precise to less precise. It is a little weird, Java will convert an int to a double if needed automatically because no information is lost. Java will not convert a double to an int automatically because rounding is involved and you are going from a more precise representation to a less precise representation. IMO it is kind of a stupid way to do it, but that's Java for you. I am a python programmer so a lot of these detials I don't have to worry about :).

Scanner console should also be declared static at the top. Static variables are accessible to all instances of an object, and since you are declaring it at the class level it will be accessible to all methods in the class. Also since you declared all of the methods static, any code in the methods will only be able to access static variables. That is the cause of the error.
 
Last edited:
The problem is that you're trying to use an instance variable in a static method. Instance variables (such as your console variable) are associated with a class instance -- you have to make a new LoanProgram in order for those to be accessible. Each separate new LoanProgram object that you create has its own unique console variable.

Static variables, on the other hand, are defined once per class, and are shared between all instances. Static methods are only able to access static variables, not instance variables, because they don't know about the individual instances.

The easy way to fix your program would be to define console as a static variable. The proper way really would be to take static off of every method except main, create a new LoanProgram object in main, and then call getRate, getYears, findPayment and displayPayment on the LoanProgram object.
 
The problem is that you're trying to use an instance variable in a static method. Instance variables (such as your console variable) are associated with a class instance -- you have to make a new LoanProgram in order for those to be accessible. Each separate new LoanProgram object that you create has its own unique console variable.

Static variables, on the other hand, are defined once per class, and are shared between all instances. Static methods are only able to access static variables, not instance variables, because they don't know about the individual instances.

The easy way to fix your program would be to define console as a static variable. The proper way really would be to take static off of every method except main, create a new LoanProgram object in main, and then call getRate, getYears, findPayment and displayPayment on the LoanProgram object.

For a program this small I wouldn't even break it out into any methods, I would do everything in main. But yes, technically the Java way of doing things would mean creating an object in main and then calling each method.
 
For a program this small I wouldn't even break it out into any methods, I would do everything in main. But yes, technically the Java way of doing things would mean creating an object in main and then calling each method.

That's not just the Java way, that's the OO way. Yeah you're right, all the methods aren't really necessary, just one would suffice.
 
For a program this small I wouldn't even break it out into any methods, I would do everything in main. But yes, technically the Java way of doing things would mean creating an object in main and then calling each method.

its for school, its a method excercise, and tyvm for the help
 
in the future use the
Code:
 tags not the [quote] tags
 
Back
Top