The main problem that you are facing is that you will have an unknown number of values that you want to print, but you use an array to store them, and the arrays have a fixed size. Since you have an int array, it will be completely populated with a default zero value.
Ideally, you should print only the first group of nonzero values in your array, but you keep the divisors scattered throughout the array.
dvr[x] = x; stores each value with an index of this value, when you really just need to save each new value in the next open spot in the array.
Create a separate index variable and save each value using instead:
int index = 0; while (x >= 2 && x <= d) { ... if (y == 0) { dvr[index++] = x; ...
Then, when your main loop is done, you can create a new “display array” that contains only dividers, not zeros. At this point, index tells you how much it should be:
int[] display = Arrays.copyOf(dvr, index); JOptionPane.showMessageDialog(null, "The divisors of " + i + " are:\n" + Arrays.toString(display));
source share