-1

My code have no error messenges but idk why it does not show anything in the console, i'm new to Java pls help me!!

public class test {
public static void main(String[] args) {
    int[] b = {27, 15, 15, 11, 27,27, 15,27, 15,27, 15};
    mode(b);
}
public static int mode(int[] array) {
    int[] spareArray = new int[101];

    for (int i = 0; i < array.length; i++) {
        spareArray[array[i]]++;
    }

    int mode = 101;
    int count = 0;

    for (int i = 0; i < spareArray.length; i++) {
        if (spareArray[i] > count) {
            count = spareArray[i];
            mode = i;
        }
    }

    return mode;
}

}

Nhat Minh
  • 1
  • 1
  • By convention java classes start with a capital letter. - If you expect something to see on your console, where is the corresponding command? – Jörg Jun 19 '21 at 07:05
  • i thought return mode will put number on my console – Nhat Minh Jun 19 '21 at 07:08
  • You need to `System.out.print()` what you return for it to show up on the console. – rdas Jun 19 '21 at 07:11
  • First: I would never use one and the same name for both a variable and a method. 2nd: "return" returns the result of a method to the caller. 3rd: To write something to the console use "System.out.println(...)". 4th: Do you have a teacher or do you learn from a book or the net? – Jörg Jun 19 '21 at 07:13

2 Answers2

1

You haven't used any print statements. You should assign the mode value calculated from the mode function to a variable and print it.

public class Test
{
    public static void main(String[] args)
    {
        int[] b = {27, 15, 15, 11, 27,27, 15,27, 15,27, 15};
        int m = mode(b);
        System.out.println( m ); // Missing statement
    }
    public static int mode(int[] array)
    {
        int[] spareArray = new int[101];
        for (int i = 0; i < array.length; i++)
            spareArray[array[i]]++;
        
        int mode = 101;
        int count = 0;
        
        for (int i = 0; i < spareArray.length; i++)
        {
            if (spareArray[i] > count)
            {
                count = spareArray[i];
                mode = i;
            }
        }
        return mode;
    }
}

You can also print it directly as: System.out.println( mode(b) );

Feel free to ask any doubt.

GURU Shreyansh
  • 881
  • 1
  • 7
  • 19
0

Please do

System.out.print(mode(b));

Instead of

mode(b);

You should see the output in the console.

Thanks