CrazyRocker
CrazyRocker

Reputation: 3

How do I get the overall maximum value in a 2d array?

I am trying to have the program take the user's input then put it into a 2d array then I want it to find the overall maximum number from all the numbers and print it. I dont know much about 2d arrays yet so I dont where the logic error is.

Heres my code :

import java.util.Scanner;

public class ToDoTwo {
    public static void main(String args[]) {
        int numbers[][] = new int[3][3], max[][] = new int[3][3], n, temp;
        Scanner obj = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("Enter a number : ");
                numbers[i][j] = obj.nextInt();
            }
        }
        max[0][0] = numbers[0][0];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (numbers[i][j] > max[0][0]) {
                    max[0][0] = numbers[i][j];
                }
            }
        }
        for (int i = 0; i < max.length; i++) {
            for (int j = 0; j < max.length; j++) {
                System.out.print(max[i][j]);
            }
        }
    }
}

Upvotes: 0

Views: 670

Answers (3)

user16087287
user16087287

Reputation:

You can greatly simplify your code by using streams:

int[][] arr = {{1, 2, 3}, {5, 4}, {32, 23}};

int max = Arrays.stream(arr).flatMapToInt(Arrays::stream)
                .max().orElse(Integer.MIN_VALUE);

System.out.println(max); // 32

Upvotes: 0

C Chaurasia
C Chaurasia

Reputation: 11

import java.util.Scanner;

public class ToDoTwo {
    public static void main(String args[]) {
        int numbers[][] = new int[3][3], max[][] = new int[3][3], n, temp;
        Scanner obj = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println("Enter a number : ");
                numbers[i][j] = obj.nextInt();
            }
        }
        int maxVal = -Integer.MIN_VALUE;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (numbers[i][j] > maxVal) {
                    maxVal = numbers[i][j];
                }
            }
        }
        System.out.println(maxVal);
    }
}

Upvotes: 0

Matt
Matt

Reputation: 13923

Your program more or less works as you want but definitely can be improved. The result is currently just displayed with 8 zeros appended. This happens because you are looping over max to print and only the first entry is actually set.

Suggestions on how to improve:

  • Use a single integer (int max = 0;) for storing the resulting maximum. An array is not needed for this.
  • Do not write multiple declarations on the same line since this is discouraged in for Java and a potential source of bugs (hard to read).
  • Give the instance of the Scanner a meaningful name (e.g. scanner) instead of obj.
  • Use numbers.length and numbers[0].length instead of a hardcoded number to make avoid problems when changing the range later on.
  • Use Java-style array declarations (int[] name) instead of C-style array declarations (int name[]).

Your whole class could look like this:

import java.util.Scanner;

public class ToDoTwo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] numbers = new int[3][3];
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[0].length; j++) {
                System.out.println("Enter a number : ");
                numbers[i][j] = scanner.nextInt();
            }
        }
        int max = 0;
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[0].length; j++) {
                if (numbers[i][j] > max) {
                    max = numbers[i][j];
                }
            }
        }
        System.out.println(max);
    }
}

If the maximum does not need to be calculated in a separate step, you could put the if-statement directly in the first loop.

Upvotes: 1

Related Questions