Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lowest value is always 0 in 2d array

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Replace :

int low = a[i][j];

with :

int low = Integer.MAX_VALUE;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading