aspose file tools*
The moose likes Beginning Java and the fly likes add up differnt values in loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "add up differnt values in loop" Watch "add up differnt values in loop" New topic
Author

add up differnt values in loop

David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
hi,

i have the following for loop that reads from a text file (all numbers) and eventually writes to the screen, what i need to do is keep a track of the size of int count as it goes through the loop so if it does exceed awidth, the loop stops, any ideas??? at the moment it never exceeds awidth because it reads each value seperately and no value is more than 55 and awidth is nearly 200.


for (count=count;count<awidth {

count = inputFile.nextInt();
x = count;
color = inputFile.nextInt();
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

i'm confused. you say you want the loop to exit when count exceeds width. but then you state that count will never be greater than 55, while width is 200ish. so the loop shouldn't ever exit.

So what do you really want? the statement "count < width" will cause the loop to exit WHEN COUNT IS GREATER THAN OR EQUAL TO INT, but if you never give it that value, it won't end.

your statement count=count doesn't really do anything, you could leave it out


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
hi fred,

i want it to exit when it is equal or exceeds awidth i.e. it could go through the loop 4 times reading values 55,48,50,50 and after the fourth loop the value would be over 200, so i want it to exit out of the loop, what is happening at the moment is that it goes thorugh every value because the total is not being tracked from one loop to the next. hope this is a bit clearer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Do you really want a for loop at all? You are creating a sum and calling it awidth, and you want to loop while awidth is not more than 200?

Back to kinds of loops and see whether I have given a useful hint.
David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
hi campbell,

yeah that is right while it is less than 200 keep looping and when it exceeds 200 go back to the main program which will increment another integer by 1 and then back into the loop and on and on until there is no more number to read.

So you thing the for loop is wrong? do you think an if else statement maybe?

if awidth < 200 - keep looping

else increment integer by 1
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

it could go through the loop 4 times reading values 55,48,50,50 and after the fourth loop the value would be over 200

so each time it reads an input, you want to INCREASE the value of count by what you read. is that it?

you can simply do this:

count += inputFile.nextInt();

this says "read the nextInt from the input file, and add it to the current value of count". using your example above (and assuming count starts at 0), the first time through, count would be set to 55. the second to 103, the third to 153, and the fourth time to 203.
David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
cheers guys for all the help, i got it working, i used a while and then an if else statement.. i can thing these things through in my head quite easy but some of the basic logic and syntax can baffle me for hours!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Well done. A loop starting while (awidth < 200) is probably what you want.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: add up differnt values in loop