Java: Finding the highest value in an array Java: Finding the highest value in an array arrays arrays

Java: Finding the highest value in an array


It's printing out a number every time it finds one that is higher than the current max (which happens to occur three times in your case.) Move the print outside of the for loop and you should be good.

for (int counter = 1; counter < decMax.length; counter++){     if (decMax[counter] > max)     {      max = decMax[counter];     }}System.out.println("The highest maximum for the December is: " + max);


To find the highest (max) or lowest (min) value from an array, this could give you the right direction. Here is an example code for getting the highest value from a primitive array.

Method 1:

public int maxValue(int array[]){  List<Integer> list = new ArrayList<Integer>();  for (int i = 0; i < array.length; i++) {    list.add(array[i]);  } return Collections.max(list);

}

To get the lowest value, you can use

Collections.min(list)

Method 2:

public int maxValue(int array[]){  int max = Arrays.stream(array).max().getAsInt();  return max;}

Now the following line should work.

System.out.println("The highest maximum for the December is: " + maxValue(decMax)); 


You need to print out the max after you've scanned all of them:

for (int counter = 1; counter < decMax.length; counter++){    if (decMax[counter] > max)    {        max = decMax[counter];        // not here: System.out.println("The highest maximum for the December is: " + max);    }}  System.out.println("The highest maximum for the December is: " + max);