I'm just trying to sort the tabbed output by the Count and have all the other columns be sorted appropriately.
int maxNum = A6DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int rollnum;
int randomValue;
NumberFormat formatter = new DecimalFormat("#0.00");
ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();
//Using ArrayList for my Sum and Counts
System.out.println("Welcome to the Dice Roll Stats Calculator!");
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();
for (int i = 0; i < rollnum; i++) {
randomValue = (1 + (int) (Math.random() * maxNum)) + (1 + (int) (Math.random() * maxNum));
if (numbers.contains(randomValue)) {
int position = numbers.indexOf(randomValue);
counts.set(position, counts.get(position) + 1);
} else {
numbers.add(randomValue);
counts.add(1);
}
}
System.out.println("Sum\tCount\tPercentage");
System.out.println("----\t---\t----------");
for (int i = 0; i < numbers.size(); i++) {
System.out.println(numbers.get(i) + "\t" + counts.get(i)
+ "\t" + formatter.format(((double) (counts.get(i) * 100)) / rollnum) + "%");
What I need is an output that sorts my 'count' column. I'm not familiar with the sort method for Array, but since ArrayList is different, I don't know where to begin looking on how to implement it to what I've got here.
What I'm getting as an output now:
After 1000 rolls of 6-sided Dice
Sum Count Percentage
--- --- ----------
3 63 6.30%
5 116 11.60%
9 93 9.30%
7 167 16.70%
11 59 5.90%
4 85 8.50%
8 139 13.90%
10 90 9.00%
6 138 13.80%
2 27 2.70%
12 23 2.30%