Results 1 to 8 of 8
Thread: Return values from a while loop
- 04-11-2011, 04:32 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Return values from a while loop
I have this while loop
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!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++; }
- 04-11-2011, 04:46 AM #2
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.
- 04-11-2011, 04:55 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
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.
- 04-11-2011, 04:57 AM #4
How about a Person class that has a name and date of birth and an address, can you do that?
- 04-11-2011, 05:01 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
this is the mix of code and psuedocode you posted...
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 threadJava 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 } }
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?
- 04-11-2011, 05:04 AM #6
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.
- 04-11-2011, 05:05 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
Like that?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; }
- 04-11-2011, 05:12 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 25
- Rep Power
- 0
so if I do it in a method I could do it sort of like this
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?Java Code:arrayList interestList(){ return somecalculation }
Similar Threads
-
Return positive values only
By DomBrown in forum New To JavaReplies: 14Last Post: 03-31-2011, 07:48 PM -
Return values and Recursion
By blug in forum New To JavaReplies: 4Last Post: 11-14-2010, 12:55 PM -
Method to return values
By puchatek in forum New To JavaReplies: 4Last Post: 11-11-2010, 09:27 AM -
Using functions that return values?
By Megapixelz in forum New To JavaReplies: 1Last Post: 04-30-2008, 04:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks