Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Providing feedback when processing a large for loop

 
Mark Waes
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This should be easy, but for the life of me I can't figure it out...

I'm looking to provide user feeback when processing a large for-loop, and at each 10% milestone, print the message '10% Complete', '20% Complete' etc..

my code is below, but it suffers from a problem when the countTarget instance variable is an odd number (prime numbers being a specific example) in that not all of the 10% milestones are printed.

Regardless of the number of interations required by the for-loop, I would like to print feedback 10 times.



Any help gratefully received...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is you're using floats here, and floating point arithmetic is never precise. What i'd probably do is something like



You may not get your output at EXACTLY 10%, but pretty dang close.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic