int x = 0;

Also: i = x/n;
if x < n because of integer math


Try debugging the code by adding some println statements to print out the values of all the variables as they are set and used.