Results 1 to 8 of 8
  1. #1
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default 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.
    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++;
        }
    Thanks for the help!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  3. #3
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    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...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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
        }
    }

  5. #5
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    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!

  6. #6
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    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?

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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);
    }

  8. #8
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Thank you for that outline ozzyman I think I'll be able to take it from there.

Similar Threads

  1. Array or for loop? or both?
    By kedecr in forum New To Java
    Replies: 4
    Last Post: 03-09-2011, 01:24 PM
  2. Loop through Array in JSP
    By Robert_85 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-25-2010, 09:00 PM
  3. Replies: 5
    Last Post: 04-22-2010, 09:05 AM
  4. Read a Public Key from a USP stick
    By shauom in forum New To Java
    Replies: 2
    Last Post: 08-06-2008, 07:57 AM
  5. Replies: 2
    Last Post: 05-05-2008, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •