Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on Control Statement

 
Gordan Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing this exercise program for my class and I'm a mess. Please help out, any tips appreciated. I can post my codes that has errors if needed. Thanks.
I am supposed to write a program that let the user enter
loan amount:
number of years:
interest rate:
then the output is
payament # interest prncipal balance
1 58.33 806.93 9193.07
2
3
.
.
.
12
THANKS!! any tip will help.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gordan,
Welcome to JavaRanch!

I can post my codes that has errors if needed.

Please do! You'll learn a lot more if we can help you to get your own code running, than if somebody just hands you a solution.
 
Gordan Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class xxx
{
public static void main(String[] args)
{

System.out.print("Loan Amount: ");
double loanAmount = MyInput.readDouble();

System.out.print("Number of Years: ");
int numOfYears = MyInput.readInt();

System.out.print("Interest Rate: ");
int interestRate = MyInput.readInt();

System.out.println("Payment\t\tInterest\t\tPrincipal\t\tBalance");
System.out.println("-------------------------------------------------------------");

double payment;
double monthlyInterestRate;
double remainingPrincipal;
int paymentNum;
double interest;
double annualInterestRate;
double balance;

//payment #
//numOfYears = paymentNum * 12;



for (numOfYears = paymentNum * 12)
{
//calculate interest
monthlyInterestRate = annualInterestRate / 1200;

interest = monthlyInterestRate * remainingPrincipal;

//calcualte principal


//calcualte balance


System.out.println(paymentNum+"%\t\t\t"+interest+"\t\t\t"
+remainingPrincipal+"\t\t\t"+balance);

}
}
}


--------------
i know. its a mess. the codes are not done.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first thing that springs to mind is that:
numOfYears = paymentNum * 12
is not correct. Instead, you should say:

Note that this is a variation on the for-loop idiom ("for(int index = 0; index < count; index++)"); however, in this case the variation makes more sense.
Once you've done that, you should just need to perform the appropriate calculations and display the results.
If you have any further questions, just ask.
Oh, and, for our sake, when you post code, please place it between [CODE] and [/CODE] tags, it makes it easier for the rest of us to read. Thanks!
 
Gordan Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HELP!!
------

//calcualte principal
double principal = monthlyInterestRate - interest;


//calculate interest
monthlyInterestRate = annualInterestRate / 1200;

interest = monthlyInterestRate * principal;


//calcualte balance
balance = loanAmount - principal;

------------
--------------------Configuration: j2sdk1.4.0_03 <Default>--------------------
D:\xxxx\xxx.java:40: variable monthlyInterestRate might not have been initialized
double principal = monthlyInterestRate - interest;
^
D:\xxx\xxx.java:40: variable interest might not have been initialized
double principal = monthlyInterestRate - interest;
^
2 errors
Process completed.
---------------------------
[ October 02, 2003: Message edited by: Gordan Lee ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HELP!!
double principal = monthlyInterestRate - interest;
monthlyInterestRate = annualInterestRate / 1200;
D:\xxxx\xxx.java:40: variable monthlyInterestRate might not have been initialized

There is no need to yell. From the code above, it should be clear that your variable "monthlyInterestRate" is assigned an initial value after it was used, so the compiler flags it as an error.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, local (method) variables are treated differently from class member variables. Class variables get initialized to default value (0, 0.0, false, null; whichever of these is appropriate for the type). However, method variables do not get assigned a value -- not even null. So, before you can use one in a calculation or anything, you have to assign it a value (usually 0, occassionally somethign else)
Instead of:

use

And I'm just going to drop another reminder about the [CODE] and [/CODE] tags -- it really does make a difference!
[ October 02, 2003: Message edited by: Joel McNary ]
 
Gordan Lee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys for the help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic