New guy here trying 2D arrays. I have these codes but whenever I run it, the lowest value would always end up with 0 even if I input non-zero digits. Any idea how to fix this?
import javax.swing.JOptionPane;
public class TwoDArrayActivity {
public static void main(String[] args){
// declaration of array
int a[][] = new int[3][3];
int i = 0; // rows
int j = 0; // columns
String display = "";
int low = a[i][j];
for(i=0; i < a.length; i++) { // count the number of rows
for(j = 0; j < 3; j++) { // count the number of columns
a[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Enter a Number to Array [" + i + "]" + "[" + j + "]"));
if(a[i][j] < low)
low = a[i][j];
display = display + a[i][j] + " ";
} // end of inner for
display = display + "\n"; //new line
} // end of outer for
JOptionPane.showMessageDialog(null,"Values in Array\n" + display + "Lowest value is " + low, "Values in Array", JOptionPane.INFORMATION_MESSAGE);
} // end of method
} // end of class
>Solution :
Replace :
int low = a[i][j];
with :
int low = Integer.MAX_VALUE;