## How do I find the maximum number in an array in Java

Related searches

I am given the array measurements[]. I am supposed to write a for loop that goes through all the numbers and each time a number maximum is reached, the variable maximum is replaced. In the code I have so far, the output is saying that it cannot find the symbol i, but I thought that was the symbol I am supposed to use within a for-loop. Here is my code:

```    double maximum = measurements[0];
for (i = 0; i < measurements.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
System.out.println(maximum);
```

You can also do this using Java stream api :

```double maximum = Arrays.stream(measurements).max();
System.out.println(maximum);
```

Or a more concise code:

```double maximum = Double.MIN_VALUE;
for(double measurement : measurements) {
maximum = Math.max(maximum, measurement);
}
System.out.println(maximum);

```

Or, sort the array and return the last one

```Arrays.sort(measurements);
System.out.println(measurements[measurements.length-1]);
```

Java Program to find largest element in an array, Java Program to find Largest Number in an Array � public class LargestInArrayExample{ � public static int getLargest(int[] a, int total){ � int temp; � for (int i = 0; i < total; i� To find maximum and minimum values in an array in Java you can use one of the following options-. Iterate the array and look for the maximum and minimum values. See example. You can also write a recursive method to recursively go through the array to find maximum and minimum values in an array. See example.

you can try this -

```class MaxNumber
{
public static void main(String args[])
{
int[] a = new int[] { 10, 3, 50, 14, 7, 90};
int max = a[0];
for(int i = 1; i < a.length;i++)
{
if(a[i] > max)
{
max = a[i];
}
}

System.out.println("Given Array is:");
for(int i = 0; i < a.length;i++)
{
System.out.println(a[i]);
}

System.out.println("Max Number is:" + max);
}
}
```

Java Program to find Largest Number in an Array, class MinMaxExample { public static void main(String args[]){ int array[] = new int []{10, 11, 88, 2, 12, 120}; // Calling getMax() method for getting max value int� I am given the array measurements[]. I am supposed to write a for loop that goes through all the numbers and each time a number maximum is reached, the variable maximum is replaced. In the code I h

You have not declared `i` inside the for loop or before the for loop.

```double maximum = measurements[0];
for (int i = 0; i < measurements.length; i++) {  //You can declare i here.
if (array[i] > largest) {
largest = array[i];
}
}
System.out.println(maximum);
```

You can also declare i before the for loop also

Java, 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 � We can find the largest number in an array in java by sorting the array and returning the largest number. Let's see the full example to find the largest number in java array. public class LargestInArrayExample { public static int getLargest (int[] a, int total) {

Java: Finding the highest value in an array, Use if condition to compare array current value with max value, if current array value is greater than max then assign array current value as max ( max = a [i]; ).

Java – Finding minimum and maximum values in an array By Chaitanya Singh | Filed Under: Java Tutorials In this example we are finding out the maximum and minimum values from an int array.

Java Program to find largest element in an array. Last Updated: 13-06-2020. Given an array, find the largest element in it. Input : arr [] = {10, 20, 4} Output : 20 Input : arr [] = {20, 10, 20, 4, 100} Output : 100.

• `measurements` not defined, `i` not defined, `array` not defined, `largest` not defined, `maximum` not defined. We don't know that all variables `int` type or `double` type. And also you trying to get `largest` and output `maximum`. Op didn't ask for alternative answers but with his details we cannot provide exact answer for his question. This question has full of opinion based answers.