File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple variables in a loop" Watch "Multiple variables in a loop" New topic

Multiple variables in a loop

Brandon Chamberlain

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.




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

Joined: Oct 02, 2003
Posts: 11955

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:
subject: Multiple variables in a loop
It's not a secret anymore!