Question by : .java Programming help?
I’m very much new to Java and have little background knowledge with C. I have a few questions about a program I’m writing.

It is a basic principal with compounded interest every 180 days that is not accepting an principal greater than 3500 or less than 150.

First off, I’m not sure what I messed up with the syntax on the 3 listed errors below, and I’m also unsure how to set up the “if amount < 150 || > 3500 stop the program”.

Thanks to anyone with any suggestions or answers to my problems.

import java.util.Scanner;

public class LoanCalculator
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println (“Please Enter Loan Amount:”);
double amount = keyboard.nextInt();
System.out.println (“Please Enter the Number of Days:”);
int days = keyboard.nextInt();
double APR = 1.1896;
double total;
int k;
if (amount <3500 && amount > 150)
{
if (days < 22) { amount = total; } if (days > 22)
{
days / 180 = k; // my IDE – Eclipse – says “Syntax error on token “/”, invalid
AssignmentOperator”
while (k>0; k–;) // Eclipse also says “Multiple markers at this line”
{
amount * APR = amount; // Eclipse “Syntax error on token “*”, invalid AssignmentOperator”
amount = total;
}
}
}
else System.out.println (“The amount you wish to borrow is not within our limits.”);

}
}
You’re right, I don’t know why I did that in the first place, thanks.

Best answer:

Answer by Silent
You seem to be trying to do all your variable assignments backwards.

When you want to assign a value to a variable, the name of the variable goes on the left side of the = operator. The value you want to assign is on the right.

For example, you have:

days / 180 = k;

when you really mean:

k = days / 180;

You also seem to be trying to use a while loop as if it were a for loop. You probably want to read up on the different types of loops to make sure you really understand how each one works.

You should use a for loop if you want to do something a specific number of times, as you do here. A while loop is better suited to doing something until a specific condition is true — useful if what you’re doing in the loop will change that condition, or if you don’t know at the time you start the loop how many times you need to do it.

Incidentally, there is a much easier and faster way of calculating compound interest than actually looping through the amounts for each compound period. I don’t know if that’s part of your assignment or not. The classic compound interest formula is:

A = P * (1 + (r/n)) ^ (n * t)

where A is the final amount, P is the principal, r is the APR, n is the number of times per year the interest is compounded, and t is the number of years.

Add your own answer in the comments!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.