0

I am creating a little program to find the largest number in an array. The problem is that when I call my function to check for the largest value it says that it expects a class, but that doesn't really make sense for me.

Sorry for my beginner question, but I couldn't find any help elsewhere.

Here is my current code:

package challenge;

import java.util.Scanner;

class Challenge {
    public static int findMax(int arr[], int size) {
        int maxValue = arr[0];
        for(int i = 0; i < size; i++) {
            if(arr[i] > maxValue) {
                maxValue = arr[i];
            }
        }
        return maxValue;
    }

    public static void main(String[] args) {
        int numbers[] = new int[300];
        System.out.println("Enter data: ");
        Scanner scan = new Scanner(System.in);
        for(int i = 0; i < 300; i++) {
            int input = scan.nextInt();
            numbers[i] += input;
        }
        int maxValue = findMax(numbers[], numbers.length);
        System.out.println("The largest value in the array: " + maxValue);

    }
}

Thank you and have a nice day.

2
  • I'd advise you to write int[] numbers (instead of int numbers[]), as that cleary conveys that you've got a variable named numbers of type int[] (integer-array). That does not solve your problem, but you'll be more likely to spot the error once you changed that ;) Commented Aug 15, 2015 at 17:07
  • Thank you very much for the tip! Commented Aug 15, 2015 at 21:36

1 Answer 1

1

change this

int maxValue = findMax(numbers[], numbers.length);

to this

int maxValue = findMax(numbers, numbers.length);

when you pass array to a method you should not include [] brakets.you should pass the name of array numbers not numbers[]

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thank you a lot, never knew that arguments require only the data type names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.