displaying the lowest and highest of 2d array

I am trying to display the lowest and the highest value. The highest value is correct but the smallest is always giving me 0 can you help me.

package openlink.domain;

import java.io.*;

public class table2 {
    public static void main(String[] args) {
        int table1[][] = new int[5][5];
        int table2[][] = new int[5][5];
        int table3[][] = new int[5][5];
        int i, j, k, l, b, c, g, p, num = 0, num1 = 0, small = 0, largest = 0;
        String input = " ";
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (i = 0; i < 5; i++) {
            for (j = 0; j < 5; j++) {
                table1[i][j] = 0;
            }
        }
        for (k = 0; k < 5; k++) {
            for (l = 0; l < 5; l++) {
                table2[k][l] = 0;
            }
        }
        try {
            for (i = 0; i < 5; i++) {
                for (j = 0; j < 5; j++) {
                    System.out.print("Input table1[" + i + "][" + j + "]= ");
                    input = in.readLine();
                    num = Integer.parseInt(input);
                    table1[i][j] = num;
                }
            }
            for (k = 0; k < 5; k++) {
                for (l = 0; l < 5; l++) {
                    System.out.print("Input table2[" + k + "][" + l + "]= ");
                    input = in.readLine();
                    num1 = Integer.parseInt(input);
                    table2[k][l] = num1;
                }
            }
            for (b = 0; b < table3.length; b++) {
                for (c = 0; c < table3.length; c++) {

                    table3[b][c] = table2[b][c] + table1[b][c];
                }
            }
            System.out.println("table1");
            for (i = 0; i < 5; i++) {
                for (j = 0; j < 5; j++) {
                    System.out.print(table1[i][j] + "  ");
                }
                System.out.println();
            }
            System.out.println();
            System.out.println("table2");
            for (k = 0; k < 5; k++) {
                for (l = 0; l < 5; l++) {
                    System.out.print(table2[k][l] + "  ");
                }
                System.out.println();
            }
            System.out.println();
            System.out.println("table3");
            /* this is the code in how to locate the lowest and the highest */
            for (b = 0; b < 5; b++) {
                for (c = 0; c < 5; c++) {
                    System.out.print(table3[b][c] + "  ");
                }
                System.out.println();
            }
            System.out.println();
            for (b = 0; b < table1.length; b++) {
                for (c = 0; c < table2.length; c++) {
                    if (small > table3[b][c]) {
                        small = table3[b][c];
                    } else if (table3[b][c] > largest) {
                        largest = table3[b][c];
                    }

                }
            }
            System.out.println("the highest value in table3: " + largest);
            System.out.println("the lowest value in table3: " + small);
        } catch (IOException e) {
        }
    }
}

please help me here

Jon Skeet
people
quotationmark

Well, you've initialized small as 0 to start with... so you're only going to update it if there are negative numbers. Likewise you've initialized large as 0, so you'll only update that if there are positive numbers. Enter all-negative numbers and you'll find that small is right and large is wrong.

It would be better to initialize small and large as table3[0][0] to start with, so you know they're each a valid value from the array.

people

See more on this question at Stackoverflow