This is a simple programming problem. Look into mod (MODUDLUS) operations,and use mod 10. (Should be Math.mod - checkout the Math class).