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

    Default Return values from a while loop

    I have this while loop

    Java Code:
    while (loanBalance > 0)    
            MortgageBalance balance = new MortgageBalance((customInterest), interest1, principle1, payment1, loanBalance);
            //begin output and then calculate new variables
            outputTextArea.append(loancounter + "                    " + df.format(payment1) + "       " + df.format(interest1) + "         " + df.format(principle1) + "          " + df.format(loanBalance) + newline);
            interest1 = balance.interest();
            principle1 = balance.principle();
            loanBalance= balance.loanBalance();
            loancounter++;
        }
    Each time the loop iterates I want to return 3 values, the loancounter value, the interest1 value and the principle1 value to put into an arraylist. However these values as they are per iteration only exist within the while loop, right? How can I return those values out of the while loop on each iteration? Thanks a bunch!

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

    Default

    Like I told you in your other thread on this topic, you create a class to hold those values. After the calculations have been made you create an object of your class, pass the values into the object. Then you can do whatever you like with the object. Store it in an array, a List, a database etc.

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

    Default

    I apologize Junky I guess I still don't understand then. I got the arraylist and I also got the way to print the values in the arraylist. I also think I figured out how to sum all the values of the arraylist, except I guess I'm still just missing what you're saying.
    Where would I create a class to hold those values if they only exist within the while loop? Do I store those values into the object within the while loop and then return the object out of the while loop? I'm very confused and clearly not the brightest light in the house here, can you perhaps post a bit of psuedocode relating to my while loop on how to create that object? I'll go back and reread the other thread as well but I'm very confused.

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

    Default

    How about a Person class that has a name and date of birth and an address, can you do that?

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

    Default

    this is the mix of code and psuedocode you posted...

    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
        }
    }
    In this I get the class foo, how do I declare the list outside of the loop, and then how do I add Foo to that list? What I've been doing is something like what ozzyman posted in the previous thread

    java.util.List<Double> myList = new java.util.ArrayList<Double>();

    for (int i=0; i<length; i++) {
    if (something==true) {
    myList.add(someDouble);
    }
    }

    but that won't work here as I can get those values added to the array but they are only in the while loop, so I could add a line like
    if (loancounter <= customTerm * 12){
    myList.add(interest1);}
    but then how do I get the values out of the while loop?

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

    Default

    It depends on how/where you are doing the calculations. You can do it in a method that returns an array or a List. You can declare the array/List as an instance variable.

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

    Default

    Java Code:
    public class Person
    {
        String name;
        String dob;
        String address;
      
        public Person (String n, String db, String a)
        {
          name = n;
          dob = db;
          address = a;
        }
    Like that?

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

    Default

    so if I do it in a method I could do it sort of like this

    Java Code:
    arrayList interestList(){
    return somecalculation
        }
    Is that correct? What sort of calculation would you do to loop through a bunch of things to return them out to a list? I think what this means then is I can't do the while loop the way I've been doing it, I need to do each calculation in a separate thing, one for interest, one for principle and one for loancounter and then somehow put them together? Is that correct?

Similar Threads

  1. Return positive values only
    By DomBrown in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 07:48 PM
  2. Return values and Recursion
    By blug in forum New To Java
    Replies: 4
    Last Post: 11-14-2010, 12:55 PM
  3. Method to return values
    By puchatek in forum New To Java
    Replies: 4
    Last Post: 11-11-2010, 09:27 AM
  4. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 04:07 AM

Posting Permissions

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