Are you aware of the modulo, aka remainder operator % ?
Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

db