I’m currently working on a program that will calculate a simple interest rate and monthly payment taken from a loan. Although, I am running into one pretty big problem. I am trying to make it so that my principal value(see in code) is re-assigned the value of my new balance(see in code). Here is my code right now, I will explain in better detail under it:
import java.util.Scanner;
public class Payments {
public static double principal; //principal
public static double annualrate; //annual interest rate
public static double p; //monthly payment
public static double mr; //monthly interest rate
public static double nb; //new balance after monthly payments
public static double i; //interest (monthly)
public static String spaces = " "; //spaces for making clean columns
public static int months = 12;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Principal: $");
principal = input.nextDouble();
System.out.println("Anual Interest Rate: ");
annualrate = input.nextDouble();
System.out.println("Monthly Payment: $");
p = input.nextDouble();
calculate();
}
public static void calculate() {
mr = annualrate / 12;
i = mr * p;
nb = principal + i - p;
System.out.print("Month Principal Amt. Interest Payment New Balance");
System.out.println();
System.out.println();
for(int x = 1; nb > 0; nb = principal + i - p){
System.out.println(x + spaces + p + spaces + i + "%" + spaces + "$" + p + spaces + "$" + nb);
p = (Double)null;
p = nb;
}
}
}
So as you can most likely see by the comments in the code, all of the variables are shown. Now, disregard the null and me casting it to a double because that was the last thing that I tried to do before asking you guys 🙂 anyways, my final question is, how can I go about re-assigning the value of principal to my new balance (nb)? Also, a side question, would a while-loop be better for this kind of program?
This has already been said in the comments, but the most immediate problem that I can see is in
your condition,
nb > 0will never ever change in that loop.This means 1 of 2 things
judging by how you’re calculating
nb(new balance) I’m assuming that it’s going to be above 0 most of the time, and that your loop will never stop.As things are right now, I’m not entirely sure what you actually want to do in that
forloop or whether you even want a for loop at all, as that part is unclear.what is the
forloop supposed to do?it almost looks like you’re attempting to do something like