1

Here is the code snippet that I am working on and my goal is to find the largest value from the list using predefined Java methods.

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a list of integers: ");
        int array[] = new int[10];

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

        for (int j = 0; j < array.length; j++) {
            if (j < array.length) {
                int maximum = Math.max(array[j], array[j + 1]);
            }
        }

        System.out.println("Largest number of the list: " + maximum);
    }
}

The error that I am getting is the following:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
        maximum cannot be resolved to a variable

How can I resolve this issue?

0

4 Answers 4

2

You may just iterate the array of numbers and keep track of the largest value seen:

int largest = Integer.MIN_VALUE;

for (int j=0; j < array.length; j++) {
    if (array[j] > largest) {
        largest = array[j];
    }
}

Note: The above snippet assumes that you have at least one number in the input array.

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

Comments

0
  1. You can use IntStream.max() method to find the maximum element of a stream of int primitives:

    int[] arr = {4, 7, 5, 3, 2, 9, 2, 7, 6, 7};
    
    int max = Arrays.stream(arr).max().getAsInt();
    
    System.out.println(max); // 9
    
  2. You can use Stream.max(Comparator) method to find the maximum element of a stream of Integer objects:

    List<Integer> list =
            Arrays.asList(6, 10, 2, 1, 6, 4, 5, 8, 9, 8);
    
    Integer max2 = list.stream()
            .max(Comparator.comparingInt(Integer::intValue))
            .get();
    
    System.out.println(max2); // 10
    

See also:
How to find first 5 highest value in a two dimensional array?
Selection sort of array in Java

Comments

0

As said above, pay attention to the variables scope:

public class Test {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a list of integers: ");

        int array[] = new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = s.nextInt();
        }

        int maximum = 0;
        for (int j = 0; j < array.length - 1; j++) {
            maximum = Math.max(array[j], array[j + 1]);
        }

        System.out.println("Largest number of the list: " + maximum);
    }
}

Comments

0

You're defining your maximum variable inside the for loop block, making it a local variable, then you're trying to access the value on this variable outside of its definition block, that is why Java cannot find such variable, because it does not exist on that scope. Try This:

public class Test {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter a list of integers: ");
        int array[] = new int[10];
        int maximum = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = s.nextInt();
        }

        for (int j = 0; j < array.length; j++) {
            if (j < array.length) {
                maximum = Math.max(array[j], array[j + 1]);
            }
        }

        System.out.println("Largest number of the list: " + maximum);
    }
}

Try reading this link for a further explanation on scopes and variables.

Comments

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.