-1

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%
J Ben
  • 127
  • 9
  • 2
    It would be simpler if you could encapsulate the data into a single object and maintain in a single `List`. Once solution would be to make a proxy `List`, which maintain the index where the entry appeared in the `counts` `List` - [as a conceptual example](https://stackoverflow.com/questions/25880500/sorting-multiple-arrays-simultaneously/25880585#25880585) – MadProgrammer Dec 11 '17 at 23:06
  • I'm working on implementation that MadProgrammer just explained, will post if no one else answers by the time I'm done. – whatamidoingwithmylife Dec 11 '17 at 23:10

1 Answers1

0

Assuming both the lists have the same size i.e. numbers and counts - you should sort the counts list first using Collections.sort() API and then iterate over the counts list instead of numbers

Collections.sort(counts);
for (int i = 0; i < counts.size(); i++) {
System.out.println(numbers.get(i) + "\t" + counts.get(i)
                    + "\t" + formatter.format(((double) (counts.get(i) * 100)) / rollnum) + "%");           

For further readings on how to sort a list and why it's different than Set or Map, go through this post Why is there no SortedList in Java?

Pankaj Gadge
  • 2,748
  • 3
  • 18
  • 25
  • 1
    If you're sorting `counts`, but you're not applying the changes to `numbers`, then you've lost the order of the data. `count.get(i)` might not reflect the count of `number.get(i)`. – Obicere Dec 11 '17 at 23:33
  • Hmm, This sorts my columns, correctly but my percentages and sum aren't really being sorted with it. I'm receiving outputs that say rolls like "snake-eyes" are the most probable outcome, and that is mathematically impossible. Edit @Obicere Yes that's exactly what's going on. – J Ben Dec 11 '17 at 23:35
  • In that case, `Map` makes more sense as an underlying data structure that holds Number=>Count as key/value pair – Pankaj Gadge Dec 11 '17 at 23:35
  • @PankajGadge I'm not sure I follow, would you be able to show me? – J Ben Dec 11 '17 at 23:40
  • I think a better suggestion would be to drop the `numbers` altogether. With 1 dice there are only 6 possible outcomes. With 2 dice there are only 12. With 3 there are 18. etc... Therefore, using a list for `numbers` is unneeded, you can simply do: `count.get(randomValue - 1)` to get the count that reflects the dice roll. Although, this also seems like an assignment and profs can be weird with restrictions. – Obicere Dec 11 '17 at 23:40
  • @Obicere In my main class the user is asked to input the number of sides on the dice, that's why I can't just use multiples of 6. Otherwise i'd definitely go with that. Right now I'm just running tests with 6 because I understand the possible outcomes. – J Ben Dec 11 '17 at 23:47