Results 1 to 8 of 8
- 04-07-2011, 09:57 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Stick solutions of a loop into an array
I am trying to write a program which will create a loan amoritization table. The loan table will basically be such that there is a loan balance, an interest paid per month and a principle paid per month. These values change with every iteration of the loop. I'm looking for a loop, or a way to add these values into an array, as well as continue with the values so the loop continues to operate. Below is the loop I'm using for creating the loan amoritization table. Eventually I want to take these values from the array and use them to create a graph. The initial loan variables are not fixed, they are relative to user input, so I don't know how big each array will be given the user input. Here's is the current loop I'm using.
Thanks for the help!Java Code:while (loanBalance > 0) //Loop condition { MortgageBalance balance = new MortgageBalance((customInterest), interest1, principle1, payment1, loanBalance); //creates new instance of loan balance class which contains methods for calculating balance, principle and interest paid //begin output and then calculate new variables outputJTextArea.append(loancounter + " " + df.format(payment1) + " " + df.format(interest1) + " " + df.format(principle1) + " " + df.format(loanBalance) + newline); //Outputs loan amoritization table to JTextArea interest1 = balance.interest(); //method to calculate interest paid principle1 = balance.principle(); //method to determine principle paid loanBalance= balance.loanBalance();//method to determine remaining loan balance loancounter++; }
- 04-07-2011, 11:03 PM #2
Create a class to hold the values you want. Inside the loop, everytime you calculate those values create an object of the class and store it in a List.
- 04-07-2011, 11:34 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
I'm sorry I'm not following you. Could you post a quick example of what you mean? I'm real new to Java and am at a loss as to how I would go about doing that...
- 04-07-2011, 11:38 PM #4
Java Code:class Foo { int value; Foo(int v) { value = v; } } class Bar { public void main(String[] args) { // mix of code and pseudocode declare List loop { int number = perform calculation Foo = new Foo(number); add Foo to List } all Foo objects are available since they are stored in the List } }
- 04-09-2011, 01:05 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Ok so in your code the initial class foo has a method Foo(int v), this is the constructor for the class, right?
Then in the declare list, what do you mean there? I need to create an array so I'm going to create a 2D array with 2 columns, since I only need principle and interest. I'll have an unknown number of rows, as the number of payments will change based on the inputs given by the user. How do I create an array with an unknown number of rows? What is the code to create an array with an unknown number of rows and then add rows as needed? I think I understand the rest but that's where I'm lost. Thanks for your help!
- 04-09-2011, 01:21 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Ok, I found out what I want is an arraylist, but the specifics of how to use it are sort of escaping me currently. Seemingly it's something like arraylist<Double> = new arrayList, is that correct?
-
use the raw type List
java.util.List<Double> myList = new java.util.ArrayList<Double>();
for (int i=0; i<length; i++) {
if (something==true) {
myList.add(someDouble);
}
}
for (Double d:myList) {
System.out.println(d);
}
- 04-10-2011, 09:35 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Array or for loop? or both?
By kedecr in forum New To JavaReplies: 4Last Post: 03-09-2011, 01:24 PM -
Loop through Array in JSP
By Robert_85 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-25-2010, 09:00 PM -
SQL query won't stick in the database :confused:
By alexander.s in forum JDBCReplies: 5Last Post: 04-22-2010, 09:05 AM -
Read a Public Key from a USP stick
By shauom in forum New To JavaReplies: 2Last Post: 08-06-2008, 07:57 AM -
Need another program with and if-else, array, and for loop
By Zebra in forum New To JavaReplies: 2Last Post: 05-05-2008, 01:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks