In order to work, you could only print after making all the postings. To do this, you would need to save everything that you accounted for and then just print the variable that stored those postings. One possible solution would be to adjust your code to the following:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[4];
List<Integer> numerosJaContabilizados = new ArrayList<Integer>();
Map<Integer, Integer> numeroRepeticoes = new HashMap<Integer, Integer>();
int a;
for (int i = 0; i < array.length; i++) {
System.out.println("Digite um numero: ");
a = scan.nextInt();
array[i] = a;
}
System.out.println("Seu array ficou: " + Arrays.toString(array));
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j && !numerosJaContabilizados.contains(array[i])) {
registrarNumeroRepeticao(array[i], numeroRepeticoes);
}
}
numerosJaContabilizados.add(array[i]);
}
for (Entry<Integer, Integer> numeroRepeticao : numeroRepeticoes.entrySet()) {
System.out.println(
"O numero " + numeroRepeticao.getKey() + " é repetido " + numeroRepeticao.getValue() + " vezes.");
}
}
private static void registrarNumeroRepeticao(int numero, Map<Integer, Integer> numeroRepeticoes) {
if (numeroRepeticoes.get(numero) != null)
numeroRepeticoes.put(numero, numeroRepeticoes.get(numero) + 1);
else
numeroRepeticoes.put(numero, 2);
}
Changes made:
Added a variable to store the number repetitions: numberRepeats. Note that it is a map because there you can use the key as the number and as value the repeats.
Added a variable to count the numbers already processed: numberAccount. Your print while would be more or less for this, but the logic would break if the repeated number did not come in sequential order. For this it is better to use a variable that stores these postings and is used to check whether the number should be counted or not.
Added impression of values OFF of loops. This will print the result only after processing everything.
This was an example of how your method would work, but here are some suggestions:
Refactor method: Your main method has many responsibilities, ideally it should be split between at least two different methods. For example, one responsibility could be to read the data, the other would be the counting of repetitions and the other would be the printing of the repetitions. It is an interesting exercise to try to modularize a method to the maximum. I would recommend;)
Use the collections API: I saw that you use arrays with primitive types but you lose the power that API collections gives you! Even using it you would solve your problem in a few lines of code, but there it would not be very didactic so it is better to try to implement the logic of the same zero.
I hope I have helped ^^