Help with beginners java

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
"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
//import java.util.Comparator;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.TreeMap;
//import java.util.Set;

public class quesSimulator
{
public static void main(String[] args)
{ques d=new ques(34);
int[] array1= new int[50]; //array1 contains random values
for( int i=0; i<array1.length; i++)
{
array1=d.cast();
int j=array1;
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+1);
// System.out.print(array2+ " ");
}
System.out.println();

int[] array3=new int[33]; //array3 contains [number of times that number is repeated]



//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==array1[j])
{
count=count+1;
//NOT1.put((i+1), count);
array3=count;
} System.out.println((i+1)+ " has occured " + count + " time/s ");
}

//Set<Integer> keySet =NOT1.keySet();
//for(Integer key : keySet)
// {Integer value=NOT1.get(key);
// System.out.println(key + " " + value);
//System.out.println(NOT1);


}

}



ques.java
import java.util.Random;

public class ques
{ private Random generator;
private int sides;
public ques(int s)
{ sides=s;
generator= new Random();
}
public int cast()
{ return 1+generator.nextInt(sides);}}

My current output:

run quesSimulator
{4}{12}{6}{3}{25}{30}{17}{14}{17}{9}{25}{14}{16}{5}{28}{1}{10}{27}{16}{1}{21}{34}{10}{20}{33}{19}{12}{24}{30}{3}{22}{18}{2}{24}{8}{13}{6}{4}{6}{13}{33}{21}{31}{8}{22}{3}{22}{25}{25}{29}

1 has occured 2 time/s
2 has occured 1 time/s
3 has occured 3 time/s
4 has occured 2 time/s
5 has occured 1 time/s
6 has occured 3 time/s
7 has occured 0 time/s
8 has occured 2 time/s
9 has occured 1 time/s
10 has occured 2 time/s
11 has occured 0 time/s
12 has occured 2 time/s
13 has occured 2 time/s
14 has occured 2 time/s
15 has occured 0 time/s
16 has occured 2 time/s
17 has occured 2 time/s
18 has occured 1 time/s
19 has occured 1 time/s
20 has occured 1 time/s
21 has occured 2 time/s
22 has occured 3 time/s
23 has occured 0 time/s
24 has occured 2 time/s
25 has occured 4 time/s
26 has occured 0 time/s
27 has occured 1 time/s
28 has occured 1 time/s
29 has occured 1 time/s
30 has occured 2 time/s
31 has occured 1 time/s
32 has occured 0 time/s
33 has occured 2 time/s
 

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
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,

EinEin

Active Member
Newcomer
Joined
Nov 8, 2017
Messages
42
Trophies
0
Age
31
XP
69
Country
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,

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
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.
int[][] array4=new int[33][2];
for(int i=0; i<33; i++)
{ array4[0]=(i+1); //the assignments are wrong
array4[1]=(array3);
}
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,

EinEin

Active Member
Newcomer
Joined
Nov 8, 2017
Messages
42
Trophies
0
Age
31
XP
69
Country
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.
 

EinEin

Active Member
Newcomer
Joined
Nov 8, 2017
Messages
42
Trophies
0
Age
31
XP
69
Country
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]);
        }
 

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
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]);
        }
Thanks!
Got there errors on compiling. (Ignore my directory :P )
3 errors found:
File: D:\Games\DOOM\base\quesSimulator.java [line: 45]
Error: The method sort(T[], java.util.Comparator<? super T>) in the type java.util.Arrays is not applicable for the arguments (int[][], new Comparator<int[]>(){})
File: D:\Games\DOOM\base\quesSimulator.java [line: 45]
Error: Comparator cannot be resolved to a type
File: D:\Games\DOOM\base\quesSimulator.java [line: 48]
Error: The method compare(int[], int[]) of type new Comparator<int[]>(){} must override or implement a supertype method

--------------------- MERGED ---------------------------

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

EinEin

Active Member
Newcomer
Joined
Nov 8, 2017
Messages
42
Trophies
0
Age
31
XP
69
Country
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.

 

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
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.

I wish I could, have 4 exams in the next 3 days :wacko:
 

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,526
Trophies
1
XP
2,315
Country
India
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];
You won't believe it but I actually figured it out!
Thanks a LOT for your help!
 

You may also like...

General chit-chat
Help Users
  • K3N1 @ K3N1:
    Which phone do you have
  • RedColoredStars @ RedColoredStars:
    a nearly 6 yr old moto g5 plus
  • RedColoredStars @ RedColoredStars:
    Not saying its not happening for you. Your image is right there. I just dont know why yours is so high and mine is so low. :)
  • K3N1 @ K3N1:
    It could be other factors high ram refresh rate etc
  • RedColoredStars @ RedColoredStars:
    know a good app to double check? perhaps my phones own memory usage stats is full of shit. lol
  • K3N1 @ K3N1:
    Samsung UI is probably a ram hog as it is
  • RedColoredStars @ RedColoredStars:
    Yeah. Could be a number of things. My old ass phone has a custom rom on it too so a lot of the bloat is gone, and its not using the moto UI
  • M4x1mumReZ @ M4x1mumReZ:
    @K3N1, I'm using an XCover 5
  • FAST6191 @ FAST6191:
    re youtube on phones. Newpipe is the way
    +1
  • FAST6191 @ FAST6191:
    3x playback speed and no adverts plus whole load of other things.
  • K3N1 @ K3N1:
    Until it gets a lawsuit notice
  • FAST6191 @ FAST6191:
    Not sure what path they would use for this one
  • FAST6191 @ FAST6191:
    On a different note entirely I randomly ended up on a hardware mods website. Lot of stuff for the GBA and DS it seems that I had missed entirely.
  • FAST6191 @ FAST6191:
    as well as the option to pay $2 for a surface mount standard fuse
  • Oleboy555 @ Oleboy555:
    i use re vanced
  • K3N1 @ K3N1:
    VPN YouTube premium $15 a year with a Turkey connection lol
  • M4x1mumReZ @ M4x1mumReZ:
    D, E, F, G...
  • cearp @ cearp:
    @FAST6191 - totally agree. not sure the reasons people use vanced/revanced over newpipe.
    I guess it's because of people still wanting to use their yt account (with vanced)?
    +1
  • cearp @ cearp:
    when I use newpipe, I am not signed in, no ads, have exportable watch lists etc... really good.
  • luotesi @ luotesi:
    Does anyone have a golden cheat for D2R v1.0.2.0?Like Money、Exp。
  • M4x1mumReZ @ M4x1mumReZ:
    Look elsewhere or make your own
  • Psionic Roshambo @ Psionic Roshambo:
    With hookers and booze!!!
    Psionic Roshambo @ Psionic Roshambo: With hookers and booze!!!