# Help with beginners java

#### raystriker

##### The powers that be
OP
Member
"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; //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; //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; //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
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
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();

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
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;
for(int i=0; i<33; i++)
{ array4=(i+1); //the assignments are wrong
array4=(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
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.

#### raystriker

##### The powers that be
OP
Member
In your code, there's already Hashmap implemented code so that's why I suggested you to go with it.
I'm all for it, just not knowledgeable enough to implement it for my case.

#### EinEin

##### Active Member
Newcomer
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; // 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; // 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; // array3 contains [number of times that number is repeated]

int[][] array4 =new int;

// 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] = (i + 1);
array4[i] = (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 - o2;
}
});

for (final int[] i : array4) {
System.out.println(i + " " + i);
}``````

#### raystriker

##### The powers that be
OP
Member
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; // 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; // 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; // array3 contains [number of times that number is repeated]

int[][] array4 =new int;

// 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] = (i + 1);
array4[i] = (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 - o2;
}
});

for (final int[] i : array4) {
System.out.println(i + " " + i);
}``````
Thanks!
Got there errors on compiling. (Ignore my directory )
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
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. In case you're stuck again, here's the answer.

#### raystriker

##### The powers that be
OP
Member
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. In case you're stuck again, here's the answer.

I wish I could, have 4 exams in the next 3 days #### EinEin

##### Active Member
Newcomer
I wish I could, have 4 exams in the next 3 days I see. Good luck with your exams. 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 - o1;``

#### raystriker

##### The powers that be
OP
Member
I see. Good luck with your exams. 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 - o1;``
You won't believe it but I actually figured it out!
Thanks a LOT for your help!

#### EinEin

##### Active Member
Newcomer
You won't believe it but I actually figured it out!
Thanks a LOT for your help!
Well done! That's some progress there. No problem, glad I was able to help.

• Replies
2
Views
326
• • Replies
8
Views
4K
• Replies
2
Views
800
• • • Replies
11
Views
6K
Replies
0
Views
231

Help Users