Home > front end >  Collections.max gives me an error in java, can anyone please Assist?
Collections.max gives me an error in java, can anyone please Assist?

Time:10-05

here is the code:

import java.util.Collections;
import java.util.Scanner;

public class task2 {
  public static void main(String[] args) {

    int n;
    Scanner numbers = new Scanner(System.in);
    System.out.print("Enter the number of elements you want to store: ");
    // reading the number of elements from the that we want to enter
    n = numbers.nextInt();
    // creates an array in the memory of length 10
    int[] array = new int[10];
    System.out.println("Enter the elements of the array: ");
    for (int i = 0; i < n; i  ) {
      // reading array elements from the user
      array[i] = numbers.nextInt();
    }
    System.out.println("Array elements are: ");
    // accessing array elements using the for loop
    for (int i = 0; i < n; i  ) {
      System.out.println(array[i]);
    }

    System.out.println("The max value is: "   Collections.max(numbers));
  }
}

I am getting an error saying "The method max(Collection<? extends T>) in the type Collections is not applicable for the arguments (Scanner)"

If anyone could please help that would be great, please keep in mind that I am an absolute beginner in java, so please forgive me for for any silly mistakes that I've made.

CodePudding user response:

Collections.max(numbers)

doesn't work because numbers is a Scanner. "The max of a Scanner" doesn't make sense: it doesn't have maximum or minimum values, it's a thing that just gives you one value at a time.

You need to do something with the arrays that you read values into.

You can't do Collections.max(array), because array is an array, not a collection. You can make some arrays (those with reference-typed elements like String[], Object[] etc) into collections using Arrays.asList(...); but you can't do that with array because it's an int[]. Or rather, you can do that with array, but you get a List<int[]>, which you can't pass to Collections.max either, because int[] doesn't have a natural ordering.

What you can do instead is either:

  • Change the type of array from int[] to Integer[]: then Collections.max(Arrays.asList(...)) will work;
  • Replace int[] array with List<Integer> list, and add instead of setting elements;
  • Use IntStream.of(array).max().orElseThrow() instead.

CodePudding user response:

Maybe

public static void main(final String[] args) {

    final Scanner numbers = new Scanner(System.in);
    System.out.print("Enter the number of elements you want to store: ");
    // reading the number of elements from the that we want to enter
    final Integer n = numbers.nextInt();
    // creates an array in the memory of length 10
    final List<Integer> array = new ArrayList<>(10);
    System.out.println("Enter the elements of the array: ");
    for (int i = 0; i < n; i  ) {
        // reading array elements from the user
        array.set(i, numbers.nextInt());
    }
    System.out.println("Array elements are: ");
    // accessing array elements using the for loop
    for (int i = 0; i < n; i  ) {
        System.out.println(array.get(i));
    }

    System.out.println("The max value is: "   Collections.max(array));
}
  • Related