Return to start of loop java - java

This is a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8.
I may have over-indulged myself here as I'm pretty new to programming, but I'm in too deep and want to figure this out.
With my current code the first line outputs correctly, displaying the rate, total and monthly. However, after that the code just continues to output the inner most loop. I need to return to the start of the loop. It would greatly be appreciated if you could point me in the right direction.
P.S. I am aware that my padding isn't in place. My biggest concern is getting the algorithm down on "paper" first, and then worrying about the beauty of it.
package loancalc194;
import java.util.Scanner;
public class LoanCalc194 {
public static void main(String[] args) {
//LOAN CALCULATOR
//user enters loan AMOUNT, loan PERIOD(years),
//output displays MONTHLY and TOTAL payments
//^displayed per 1/8 increment from 5-8% interest
Scanner in = new Scanner(System.in);
//prompt user
//retrieve loan amount and period
System.out.print("Please enter the loan amount: ");
double amount = in.nextDouble();
System.out.print("Please enter the loan period (years): ");
int period = in.nextInt();
//INTEREST LOOP///////////////////////////////////////////////////
double interest = .05000;
double inc = 0.00125;
interest = interest + inc;
double monthly = 0;
double total = 0;
System.out.println("Interest Rate\t\t\tTotal Payment\tMonthly Payment");
while (interest < .08){
//interest = interest + inc;
System.out.print(interest + "\t");
while (true){
total = (amount * period) + (interest * amount);
System.out.print("\t" + total + "\t\t");
while (true) {
monthly = ((total / period) / 12);
System.out.println(monthly);
//interest = interest + inc;
}
}
}

One loop should be enough for what you want.
Your two while(true) loops do nothing else than looping on the same values forever.
In the following code, the interest gets incremented and the new calculations computed at each loop until interest reaches your max value, this is probably what you need.
double interest = .05000;
double inc = 0.00125;
double monthly = 0;
double total = 0;
while (interest < .08){
System.out.print(interest + "\t");
total = (amount * period) + (interest * amount);
System.out.print("\t" + total + "\t\t");
monthly = ((total / period) / 12);
System.out.println(monthly);
interest = interest + inc;
}

Related

Java Loan Calculator nested for loop based on user input

The task is to create a loan calculator based on the user input of min and max years of loan payments, loan amount, and min and max % rate with incremented value given by user as well for rate and number of years.
Desired output should look like this:
Principle: $275000.0
Years to repay: 10
Interest Monthly
Rate Payment
6.25 3087.7
6.75 3157.66
7.25 3228.53
Principle: $275000.0
Years to repay: 15
Interest Monthly
Rate Payment
6.25 2357.91
6.75 2433.5
7.25 2510.37
Principle: $275000.0
Years to repay: 20
Interest Monthly
Rate Payment
6.25 2010.05
6.75 2091.0
7.25 2173.53
Please help me fix errors. Thanks!
public static void main(String[] args){
Scanner console = new Scanner (System.in);
System.out.println("This program computes monthly " + "mortgage payments.");
System.out.print("Enter the loan amount: ");
double loan = console.nextDouble();
System.out.print("Enter the starting number of years to repay the loan: ");
int startingYears = console.nextInt();
System.out.print("Enter the ending number of years to repay the loan: ");
int endingYears = console.nextInt();
System.out.print("Enter the years increment between tables: ");
int incrementYears = console.nextInt();
System.out.print("Enter the starting loan yearly interest rate, %: ");
double startingRate = console.nextDouble();
System.out.print("Enter the ending loan yearly interest rate, %: ");
double endingRate = console.nextDouble();
System.out.print("Enter the increment interest rate, %: ");
double incrementRate = console.nextDouble();
System.out.println();
System.out.println("Principle: $" + (double) loan);
System.out.printf("Years to repay: %d\n", startingYears);
System.out.println("-------- -------");
double payment;
System.out.println("Rate\tPayment");
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++){
int n = 12 * startingYears;
double c = startingRate / 12.0 / 100.0;
payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 +c, n) - 1);
System.out.println(i + " " + payment);
// System.out.println(round2(startingRate) + "\t" + round2(payment));
startingYears += incrementYears;
}
}
}
}
for (int j = startingYears; j <= endingYears; incrementYears++) {
for (double i = startingRate; i <= endingRate; incrementRate++) {
You are never incrementing the values of your counters i.e. i and j and hence the values of i and j will always be equal to the initialized values and these loops will run for infinite times.
Increment both the counters to be able to reach the termination condition i.e. j <= endingYears and i <= endingRate respectively.
Here is the code snippet:
System.out.println("Principle: $" + (double) loan);
for (int j = startingYears; j <= endingYears; j+=incrementYears) {
System.out.printf("Years to repay: %d\n", j);
System.out.println("-------- -------");
System.out.println("Rate\tPayment");
for (double i = startingRate; i <= endingRate; i+=incrementRate){
int n = 12 * j;
double c = i / 12.0 / 100.0;
double payment = loan * c * Math.pow(1 + c, n) / (Math.pow(1 + c, n) - 1);
System.out.println(i + "\t" + payment);
}
System.out.println();
}

How to create a savings bank system where program stops looping after balance doubles

I am a tasked to create a program that runs as described, programmed in Java:
Illustrate the growth of money in a savings account. The user enters the initial amount (as a decimal) and the interest rate which are used to calculate the number of years until the money doubles and the number of years until the money reaches a million dollars. (you will need to use more than 1 loop).
Note: The balance at the end of each year is
(1 + r) * balance
where balance is the previous balance, and r is the annual rate of interest in decimal form.
However, I cannot find the error in my code. What happens is that it continuously writes "Your balance will double in " and then the number infinitely grows.
Here is the code:
Scanner input = new Scanner(System.in);
double years = 0;
double futureVaule;
System.out.print("Please enter the amount of money you would like to deposit, as a decimal:\n>>");
double balance = input.nextDouble();
System.out.print("Please enter the interest rate of your deposit, as a percent:\n>>");
double r = input.nextDouble();
do
{
futureValue = (1 + r) * balance;
years = years + 1;
System.out.println("Your balance will double in "
+ Math.round(years) + " year(s).");
}while(futureValue <= balance*2);
This should work
futureValue = balance;
double doubleBalance = balance*2;
while(futureValue <= doubleBalance)
{
futureValue += futureValue * (( 1 + r)/100);
years++;
}
System.out.println("Your balance will double in "
+ years + " year(s).");

Java - Assigning User Input to Variable / Change Counter

I'm quite new to java, although I have a fairly basic knowledge of C++.
For my assignment I am counting change and sorting it into American currency (i.e., if you had 105 cents, it would divide it into one dollar and one dime).
Logically I understand how to do this, but I'm having some serious trouble understanding the java syntax. I'm having serious trouble to find a way to assign a user-inputted value to a variable of my creation. In C++ you would simply use cin, but Java seems to be a lot more complicated in this regard.
Here is my code so far:
package coinCounter;
import KeyboardPackage.Keyboard;
import java.util.Scanner;
public class helloworld
{
public static void main(String[] args)
{
Scanner input new Scanner(System.in);
//entire value of money, to be split into dollars, quarters, etc.
int money = input.nextInt();
int dollars = 0, quarters = 0, dimes = 0, nickels = 0;
//asks for the amount of money
System.out.println("Enter the amount of money in cents.");
//checking for dollars, and leaving the change
if(money >= 100)
{
dollars = money / 100;
money = money % 100;
}
//taking the remainder, and sorting it into dimes, nickels, and pennies
else if(money > 0)
{
quarters = money / 25;
money = money % 25;
dimes = money / 10;
money = money % 10;
nickels = money / 5;
money = money % 5;
}
//result
System.out.println("Dollars: " + dollars + ", Quarters: " + quarters + ", Dimes: " + dimes + ", Nickels: " + nickels + ", Pennies: " + money);
}
}
I would really appreciate some help with how to assign a user-input to my variable, Money. However, if you see another error in the code, feel free to point it out.
I know this is really basic stuff, so I appreciate all of your cooperation.
Change this line :
Scanner input new Scanner(System.in);
To :
Scanner input = new Scanner(System.in);
And this should be after line below not before:
System.out.println("Enter the amount of money in cents.");
And as you did , the line below will read from input int value and assign it to your variable money :
int money = input.nextInt();

calculating interest on a certificate of deposit

I'm working on a program that will calculate the basic interest accrued on a certificate of deposit. The program asks for the amount of money invested and the term (up to five years). Depending on how many years their term is, is what determines how much interest is earned. I use an if/else statement to determine the rate of interest. I then use a loop to print out how much money is in the account at the end of each year. My problem is that when I run the program, the money is not counting.
Here is the entire code.
import java.util.Scanner;
public class CDCalc
{
public static void main(String args[])
{
int Count = 0;
double Rate = 0;
double Total = 0;
Scanner userInput = new Scanner(System.in);
System.out.println("How much money do you want to invest?");
int Invest = userInput.nextInt();
System.out.println("How many years will your term be?");
int Term = userInput.nextInt();
System.out.println("Investing: " + Invest);
System.out.println(" Term: " + Term);
if (Term <= 1)
{
Rate = .3;
}
else if (Term <= 2)
{
Rate = .45;
}
else if (Term <= 3)
{
Rate = .95;
}
else if (Term <= 4)
{
Rate = 1.5;
}
else if (Term <= 5)
{
Rate = 1.8;
}
int count = 1;
while(count <= 5)
{
Total = Invest + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}
}
}
and here is the result I get with a 10 dollar investment, just to keep it simple, and a 5 year investment.
How much money do you want to invest?
10
How many years will your term be?
5
Investing: 10
Term: 5
Value after year 1: 10.18
Value after year 2: 10.18
Value after year 3: 10.18
Value after year 4: 10.18
Value after year 5: 10.18
My main problem is I dont know how to make it continually add the intrest onto the total. I'm not sure if I need to use a different loop or what. Any help would be appreciated.
Total = Invest + (Invest * (Rate) / (100.0));
You are not changing the value of Invest for each year, so it is not compounding. It is like you are getting .18$ of interest each year, retired from the account.
Change Total for Invest.
You need to add the investment interest to your total:
Total = Invest;
int count = 1;
while(count <= 5)
{
Total = Total + (Invest * (Rate) / (100.0));
System.out.println("Value after year " + count + ": " + Total);
count++;
}

Math.pow difficulty

When I enter 1000 for investment amount 4.25 for monthly interest rate and 1 for years, why do I get the result 4.384414858452464E11 instead of the expected 1043.34?
import java.util.Scanner;
public class FinancialApplicationFutureInvestment_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
int investmentAmount = input.nextInt();
System.out.println("Enter monthly interest rate: ");
double monthlyInterestRate = input.nextDouble();
System.out.println("Enter number of years");
int numOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
(Math.pow(1 + monthlyInterestRate, numOfYears * 12));
System.out.println("Accumulated value is: " + futureInvestmentValue);
double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
System.out.println(test);
}
}
Monthly interest rate will probably need to be entered as 0.0425
1 + monthlyInterestRate
Is monthlyInterestRate a raw factor, or is it expressed in percentage points?
Try dividing by one hundred.
the formula is
A = P(1+r/100)^n
so it should be
investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12));
well, you compute 1+4.25 (5.25) as monthly interest rate, instead of 1+(4.25/100) .
you should use casting in System.out.println (double to int) --
(int)(futureInvestmentValue * 100) / 100.0

Categories