Help with beginners java

raystriker

The powers that be
OP
Member
Joined
Dec 28, 2011
Messages
1,528
Trophies
1
XP
2,607
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,528
Trophies
1
XP
2,607
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
44
Trophies
0
Age
32
XP
80
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,528
Trophies
1
XP
2,607
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
44
Trophies
0
Age
32
XP
80
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
44
Trophies
0
Age
32
XP
80
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,528
Trophies
1
XP
2,607
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
44
Trophies
0
Age
32
XP
80
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,528
Trophies
1
XP
2,607
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,528
Trophies
1
XP
2,607
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!
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • K3Nv2 @ K3Nv2:
    Batocera is nice everything pre set up no hassles of bios shit or what not
  • BigOnYa @ BigOnYa:
    Sometimes even porn bores you. I remember when I was a kid, they has a Co. that would make books with your name in the story to keep you entertained, and deliver them to you r house. We need a interactive porn video with us inside the video, like her saying "Yea Ken, spank my ass" kinda shit. As a siloute of "Ken" shows in the foreground.
  • K3Nv2 @ K3Nv2:
    Well your wife's next to you have her prerecorded voice overs first I believe in good quality porn
  • BigOnYa @ BigOnYa:
    She does have good sound effects. Well you know.
  • K3Nv2 @ K3Nv2:
    Many years of fake practices
  • BigOnYa @ BigOnYa:
    True dat.
  • BigOnYa @ BigOnYa:
    Beans and Bologna. What gurl wouldn't want to come home to that.
  • K3Nv2 @ K3Nv2:
    Guess my sister had a new baby at this point the doctors are like well see you again in 9 months
  • BigOnYa @ BigOnYa:
    Damn i should of wrapped it that time... hope she has her hubby fooled:P
  • BigOnYa @ BigOnYa:
    Nuh that's cool, you gonna be an again Uncle
  • K3Nv2 @ K3Nv2:
    Screw that family I stay away as much as I can
  • BigOnYa @ BigOnYa:
    Nuh, Fam 4 Life, no ducking that one
  • K3Nv2 @ K3Nv2:
    If you knew mine you'd run
  • BigOnYa @ BigOnYa:
    All fams have issues, mine too. No matter, it's still Fam. (But I know what you mean)
  • Veho @ Veho:
    Fam as in family?
  • BigOnYa @ BigOnYa:
    Yea
  • Veho @ Veho:
    Nah, family is like the initial party in an RPG, they're just a suggestion but you can ditch them once you level up.
    +1
  • BigOnYa @ BigOnYa:
    I love that analogy
  • K3Nv2 @ K3Nv2:
    People are too closed in over the idea of blood is blood I believe people are people and you can choose the ones you want to be around easily
  • BigOnYa @ BigOnYa:
    Ofcourse you can choose, but Family is bond, no matter, you have to go out of your way for family. Golden rule. "You can choose your friends, but unfortunately, you can cannot choose your family"
  • K3Nv2 @ K3Nv2:
    Yeah but no one said you have to talk to them the only difference is sharing a name
    +1
  • K3Nv2 @ K3Nv2:
    Like how kids are born into a family business that may want to do something different and become a complete disappointment to them
    +1
  • BigOnYa @ BigOnYa:
    True. I have cousins on my dad side I've never spoke to. Only knew of them when going to Grandma's funeral. N still don't speak with them (I tried, left my # with them, have invited them to dinner-nothing) That's life
  • K3Nv2 @ K3Nv2:
    Not saying I'd wish any ill will but I don't want their drama
    K3Nv2 @ K3Nv2: Not saying I'd wish any ill will but I don't want their drama