user3269660
user3269660

Reputation: 53

How to fix my method to be display on the main method?

public class Hw7Pr2 {

    public static void main(String[] args) {
        int [] grades={40,55,70,58};

        System.out.println("best: ");
        int[] best1 = best(grades);

        for (int i = 0; i < best1.length; i++) {
            System.out.print(best1[i]);
        }
    }   

    public static int[] best(int[] grades){
        System.out.println("The best scores is: ");
        int best = grades[0];
        for (int i = 0; i < grades.length; i++) {
            if (grades[i] > best)
                best = grades[i];
            return best;
        }
    }
}

My method is not working the way I need it to work. I am getting this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from int to int[]

Upvotes: 1

Views: 43

Answers (2)

takendarkk
takendarkk

Reputation: 3442

int[] best1 = best(grades);

Here you are trying to get a single number, the highest score. For that you don't need an array of ints, just a single int. It should be

int best1 = best(grades);

Since you only have 1 number that you are getting you also don't need the for loop right after to print, just print the single int you now have.

System.out.print(best1);

Your method's logic looks fine, but your return type is incorrect. Your method finds the single largest number which means you don't need to return an array there, only a single int.

public static int[] best(int[] grades)

becomes

public static int best(int[] grades)

Also, it looks like your return statement is inside of your for loop in your method. You want that outside the method so it only returns once it has checked the whole array. All together the code will look like this

public class Hw7Pr2 {

    public static void main(String[] args) {
        int [] grades = {40, 55, 70, 58};        

        System.out.print("Best: ");
        int best1 = best(grades);
        System.out.print(best1);
    }

    public static int best(int[] grades){
        if (grades == null || grades.length < 1) { return -1; } //Should always check

        int best = grades[0];
        for (int i = 1; i < grades.length; i++) {
            if (grades[i] > best) {
                best = grades[i];
            }
        }
        return best;
    }
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201527

First, your method best sounds like a max to me. You appear to have missed a closing brace (and I recommend you always use braces). You could also make it a variadac function. Something like

public static int max(int... values) {
    if (values == null || values.length < 1) { // <-- check for null.
        return -1;
    }
    int max = values[0];
    for (int i = 1; i < values.length; i++) { // <-- I'd start at 1 (not 0)
        if (values[i] > max) { // <-- braces
            max = values[i];
        }
    }
    return max;
}

Then you could call it, and print your array with Arrays.toString(int[]), like

public static void main(String[] args) {
    int[] grades = { 40, 55, 70, 58 };
    System.out.printf("Best: %d%n", max(grades)); // <-- or, max(40,55,70,58)
    System.out.println(Arrays.toString(grades));
}

Output is

Best: 70
[40, 55, 70, 58]

Upvotes: 0

Related Questions