Help with beginners java

Discussion in 'Computer Programming, Emulation, and Game Modding' started by raystriker, Dec 12, 2017.

  1. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    "There are 33 problems to choose from. Write a program that randomly chooses one of them. Make the program choose 100 times. The program should report how many times each problem has been chosen, also the problem that was selected most. Make the program sort the problems descending by the number of times they were selected"

    I have got everything running except that I haven't been able to sort the problems in a descending order by the numbers of the times they were sorted. I looked into hashmaps and also 2d arrays. Ran out of brain power before I could figure something out successfully. Help?

    Code:
    quesSimulator.java

    ques.java
    My current output:

     
  2. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    side note, for testing i'm only using 50 random values.

    I just want to display the data sorted in a descending order with the number of times being descended and the corresponding question number being shown at the side somewhere.
     
    Last edited by raystriker, Dec 13, 2017
  3. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    You could use Hashmap with Comparator to sort the map.

    i.e:

    Code:
            Random random = new Random();
        
            HashMap<Integer, Integer> test = new HashMap<>();
        
            for (int i = 1; i <= 33; i++) {
                test.put(i, random.nextInt(5));
            }
        
        
            Set<Entry<Integer, Integer>> entries = test.entrySet();
        
            LinkedList<Entry<Integer, Integer>> entryList = new LinkedList<>(entries);
        
            Collections.sort(entryList, new Comparator<Entry<Integer, Integer>>() {
    
                @Override
                public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
                    return (o1.getValue()).compareTo(o2.getValue());
                }
            });
    
            Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>();
            for (Map.Entry<Integer, Integer> entry : entryList) {
                sortedMap.put(entry.getKey(), entry.getValue());
            }
      
        
            Iterator<Entry<Integer, Integer>> iterator = sortedMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry pair = (Map.Entry) iterator.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
            }
     
    Last edited by EinEin, Dec 12, 2017
  4. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    okay, so I decided Hashmaps were beyond the time i can invest in this question (ugh finals week)
    I came up with a 2d array that should store my numbers 1-33 and the corressponding values of how many times they've been generated through the random function.
    Using selection sort can all the volumes in the 2nd column be sorted and the corresponding values of the first column in each row be 'stuck' with them? HAAAAALP
     
    Last edited by raystriker, Dec 12, 2017
  5. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    In your code, there's already Hashmap implemented code so that's why I suggested you to go with it.

    Sure, you can sort multi-dimensional arrays using Arrays.sort.
     
  6. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    I'm all for it, just not knowledgeable enough to implement it for my case.
     
  7. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    I see, in that case I modified your code to work with arrays as you suggested. Take a look at it if you're stuck.
    Btw, I didn't want to modify too much of your code so I kept it as close as it is to the original.

    Code:
            ques d = new ques(34);
            int[] array1 = new int[50]; // array1 contains random values
            for (int i = 0; i < array1.length; i++) {
                array1[i] = d.cast();
                int j = array1[i];
                System.out.print("{" + j + "}");
            }
            System.out.println();
            int[] array2 = new int[33]; // array2 contains values 1-33
            for (int i = 0; i < 33; i++) {
                array2[i] = (i + 1);
                // System.out.print(array2+ " ");
            }
            System.out.println();
    
            int[] array3 = new int[33]; // array3 contains [number of times that number is repeated]
    
            int[][] array4 =new int[33][2];
    
            // Map<Integer, Integer> NOT1 = new HashMap<Integer, Integer>();
            for (int i = 0; i < 33; i++) {
                int count = 0;
                for (int j = 0; j < array1.length; j++)
                    if (array2[i] == array1[j]) {
                        count = count + 1;
                        // NOT1.put((i+1), count);
                        array3[i] = count;
                    }
                
                    array4[i][0] = (i + 1);
                    array4[i][1] = (count);
                System.out.println((i + 1) + " has occured " + count + " time/s ");
            }
            
            Arrays.sort(array4, new Comparator<int[]>() {
                
                @Override
                public int compare(int[] o1, int[] o2) {
                    return o1[1] - o2[1];
                }
            });
    
            for (final int[] i : array4) {
                System.out.println(i[0] + " " + i[1]);
            }
     
  8. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    Thanks!
    Got there errors on compiling. (Ignore my directory :P )
    — Posts automatically merged - Please don't double post! —

    nevermind, solved
    Edit: How do I display this in a descending order?
     
  9. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    Change the result of the compare function. Take some minutes and figure it out, programming is fun if you're able to learn new thing by yourself. :D

    In case you're stuck again, here's the answer.

     
  10. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    I wish I could, have 4 exams in the next 3 days :wacko:
     
  11. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    I see. Good luck with your exams. :D

    Not sure why, but the spoiler tag isn't showing up in my previous post so I'll post the answer here.

    Code:
                    return o2[1] - o1[1];
     
  12. raystriker
    OP

    raystriker Alpha PC Builder

    Member
    1,310
    451
    Dec 28, 2011
    India
    Bloomington
    You won't believe it but I actually figured it out!
    Thanks a LOT for your help!
     
  13. EinEin

    EinEin Member

    Newcomer
    40
    9
    Nov 8, 2017
    Vietnam
    Well done! That's some progress there. No problem, glad I was able to help.