wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Multiple variables in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple variables in a loop" Watch "Multiple variables in a loop" New topic
Author

Multiple variables in a loop

Brandon Chamberlain
Greenhorn

Joined: Oct 17, 2009
Posts: 1
instead of trying to explain all the different conditions I've tried, maybe there's a much simpler approach to the following situation:

I want to be able to input a unknown number of values, ending the list with the value 999
I then want it to take the difference of each consecutive value.
Finally, output these differences in a list.

Example

Input:
10.5
12.05
13.25
21
7
999

Output:
1.55
1.20
7.75
-14

I do not want the outputs list mixed in the inputs list... I want all the inputs listed first (as they're entered - an unknown number of inputs), the list ending with 999.
Followed by a list of outputs...
Any suggestions?

p.s. the equations and code is simple enough, just the formatting of getting all the inputs first then listing all the outputs is where I'm stuck.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

collect all your inputs into something like an Arraylist (which will grow as big as you need it).

once the user inputs 999 (don't collect that value), just iterate through the list and do the subtraction.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to javaranch Brandon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple variables in a loop