-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%
2
  • 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 Commented Dec 11, 2017 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. Commented Dec 11, 2017 at 23:10

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

6 Comments

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).
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.
In that case, Map<Integer, Integer> makes more sense as an underlying data structure that holds Number=>Count as key/value pair
@PankajGadge I'm not sure I follow, would you be able to show me?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.