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

# Thread: Beginner at Java, Need help with my project its due tuesday.

1. ## Beginner at Java, Need help with my project its due tuesday.

Guidelines

This assignment is comprised of modifying your program which will calculate a monthly a moritzed amount (e.g. a house mortgage). For this program, you are to add the logic to prompt the user if they wish to enter the data again, then loop if they enter a "y" value; otherwise perform the calculations and the error checking as in the previous program.

Enter loan amount: 100000
Enter rate: 6
Enter number years: 30

The monthly payment is: \$599.55

Would you like to calculate again (y/n): y

Enter loan amount: -100000
Enter rate: 6
Enter number years: 30

You must enter positive numeric data!

Would you like to calculate again (y/n): n

__________________________________________________ __________________

```package monthypayment2;
import java.text.NumberFormat;
import java.util.Scanner;

public class Monthypayment2 {
private static double monthlypay1;
private static String choice;
private static boolean n;
private static String False;
private static boolean y;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

//format number and currency
NumberFormat NF = NumberFormat.getCurrencyInstance();
NumberFormat NI = NumberFormat.getNumberInstance();

double months = 1;
double numyears;
double loanamount;
double rate;

int repeat = 0;   //condition for loop

do {

//prompt loan amount
System.out.print("Enter Loan Amount:");
loanamount = in.nextDouble();

//prompt rate
System.out.print("Enter Rate:");
rate = in.nextDouble();

//prompt years
System.out.print("Enter Number of Years:");
numyears = in.nextDouble();

//calculation monthly
monthlypay1 = (loanamount * rate / 12) / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);

System.out.println("The Monthly Payment is: \$" + monthlypay1);

y = true;
n = false;

if (monthlypay1 < 0) {
System.out.print("You need to enter positive numerical data!");}
else {
System.out.print("Would you like to continue calculations(y/n)?");
choice = in.nextLine();
}
} while (choice.equals("y"));

while(choice.equals("n")) {
break; }
}

}```

Basically when I run the program the Math is still off despite me getting help and doing all suggestions that users gave me on here. I compared it with the formula and it's correct.
When i put in a negative loan amount i get this error
at line 65(which would be
` while (choice.equals("y"));`
after asking if I would like to calculate again.
I've restarted a couple times on the project but I keep running into the same problems.
Can anyone help me?

https://scontent-b-iad.xx.fbcdn.net/...95&oe=526636AF <--- formula that I am using.

2. ## Re: Beginner at Java, Need help with my project its due tuesday.

`private static String choice;`
The variable choice is not initialised so it is given the default value of null. If your code never assigns a value to choice then you get the NPE. You should always initialise your variables with appropriate values.

3. ## Re: Beginner at Java, Need help with my project its due tuesday.

for the math I tried breaking it down but still doesn't calculate correctly.
```double calc1 = loan * rate;
double calc2 = Math.pow(1 + rate, months);
double calc3 = Math.pow(1 + rate, months)-1;
double calc4 = calc1 / calc2;
monthlypay1 = calc4 / calc3;```