Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I got this great piece of Code, however, I wanted to store the parts that are sy

ID: 3830110 • Letter: I

Question

I got this great piece of Code, however, I wanted to store the parts that are system.out

into a string...I am working on that here,

but I woldn't mind some help!

Thanks again. I am going to try to split up the methods on my end and output the result.

Maybe you can think of the best wat to approach this? it woud be great to have the 2d array as it is, then sent over to the mode method

Thanks again

public static void getSort(int[] order, int[] chaos) {
// a 2d array stores the first column with element number and 2nd column with frequency occurence
int[][] freq = new int[order.length][2];
for (int i = 0; i < order.length; i++) {
int count = 0;
for (int j = 0; j < chaos.length; j++) {

if (chaos[j] == order[i]) {
count++;
}
}
freq[i][0] = order[i];
freq[i][1] = count;
System.out.println("Score " + order[i] + " Occur " + count);
}
int max = 0;
// find the maxinum of occurence count
for (int i = 0; i < freq.length; i++) {
if (max < freq[i][1]) {
max = freq[i][1];
}
}
// find the max number of occurence element and print
for (int i = 0; i < freq.length; i++) {
if (max == freq[i][1]) {
System.out.println("The mode is : " + freq[i][0] + " number of occurence is : " + freq[i][1]);
}
}
}

Explanation / Answer

You can easily store in string:

System.out.println("Score " + order[i] + " Occur " + count);   

So instead of printing, assign to string:

String myStr = "Score " + order[i] + " Occur " + count;

Like this:
    System.out.println("The mode is : " + freq[i][0] + " number of occurence is : " + freq[i][1]);

    String str = "The mode is : " + freq[i][0] + " number of occurence is : " + freq[i][1];

I hope your doubt has been clear now.

If not, please comment