How to print out the number of a specific digit along with the digit itself in form "nxw." n is the frequency of the number, w is the number itself.
So, for example, if the user's input was 1 1 1. The output would be 3x1.
If the user's input was 1 1 1 at the first line and 7 7 1 1 0 at the second line. The output would be 3x1.2x7.2x1.1x0. with no spaces.
Note:
loop ends with a dot.
numbers don't have to be in a specific order
user can input as many digits as they want.
So for example, Input can be 1 1 1 at the first line 7 7 1 1 0 at the second ... etc.
This is my code so far. But I know that it's not true.
import java.util.*;
public class LaufLaengenKodierung {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int freq = 0;
int oldNum = 0;
int num = 0;
boolean first = true;
while(sc.hasNextInt()) {
int i = sc.nextInt();
if(i == oldNum) {
freq++;
num = i;
} else if(i != oldNum) {
freq = 1;
oldNum = i;
num = i;
if(first) {
first = false;
num = i;
freq = 1;
}
}
}
System.out.print(freq + "x" + num + ".");
sc.close();
}
}