简体   繁体   中英

How can I find the lowest and highest number in 2d array?

package HighLow;

import javax.swing.JOptionPane;

public class HighLow {
public static void main (String[]args) {
int [][] arr= new int[3][4];
int smallest=arr[0][0];
int largest= arr[0][0];
int i = 0;
int j = 0;
{
arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 
{
if (arr[i][j] < smallest) {
smallest = arr[i][j];
}
if (arr[0][0] > largest) {
largest = arr[i][j];
}
{
JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);        
}

}

}}}

How can I find the lowest and highest number in 2d array? I'm getting an error with my code? what seems to be wrong? I just need to find the highest and lowest among the 12 numbers user will input. here is what I have so far.

How can I find the lowest and highest number in 2d array? I'm getting an error with my code? what seems to be wrong? I just need to find the highest and lowest among the 12 numbers user will input. here is what I have so far.

You do not have a loop to take in your array and therefore are receiving an error. To take in the input of the Array use this

for (int i = 0;i<3;i++)
    for (int j = 0;j<4;j++)
        arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 

Therefore you will have the box outputted 12 times for the user to input 12 Integers and then once you have the Array just run a simple sort to get the smallest and the largest.

int smallest  = arr[0][0];
int largest = arr[0][0];

for (int i = 0;i<3;i++)
    for (int j = 0;j<4;j++) {
        if (arr[i][j]<smallest)
            smallest = arr[i][j];
        else if(arr[i][j]>largest)
            largest = arr[i][j];
    }
}

Final Code being

public class Solution {
    public static void main (String[]args) {
        int [][] arr= new int[3][4];

        for (int i = 0;i<3;i++)
            for (int j = 0;j<4;j++)
                arr[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Please enter 12 numbers: ")); 

        int smallest  = arr[0][0];
        int largest = arr[0][0];

        for (int i = 0;i<3;i++)
            for (int j = 0;j<4;j++) {
                if (arr[i][j]<smallest)
                    smallest = arr[i][j];
                else if(arr[i][j]>largest)
                    largest = arr[i][j];
            }
        }

        JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
        JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);        
    }

}

The error is mainly in the initialization part where you have assumed the element in the first row and first column to be either the minimum or maximum element in the array.

You have to initialize the minimum value of the array with a value that is sure to be higher than all the values in the array, why? because only then will there be an update to the actual minimum value of the array, so for example if you set minimum to be 1000 then if the actual minimum of the array is 300, it would get updated. But if you set your minimum to be for example 30 then 300 which is the actual minimum wont get logged and there would be an error.

The absolute opposite applies to the initialization of the largest value of the array. While initializing, safe ball park estimates usually work so carry out the initializations accordingly.

Personally, I don't think you should use JOptionPane to get data from the user and to display data to the user in a Java console application such as yours. I prefer to use Scanner .

(Below code demonstrates. Notes after the code.)

Note that calling a method of JOptionPane will launch the Event Dispatch Thread (EDT) which means that your application will not terminate – unless you add System.exit(0); .

package HighLow;

public class HighLow {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.print("Please enter 12 numbers: ");
        String line = stdin.nextLine();
        String[] numbers = line.split(" ");
        int k = 0;
        int[][] arr = new int[3][4];
        int smallest = Integer.MAX_VALUE;
        int largest = Integer.MIN_VALUE;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 4; j++) {
                arr[i][j] = Integer.parseInt(numbers[k++]);
                if (arr[i][j] < smallest) {
                    smallest = arr[i][j];
                }
                if (arr[i][j] > largest) {
                    largest = arr[i][j];
                }
            }
        }
        System.out.println("The smallest value in the Array is: " + smallest);
        System.out.println("The largest value in the Array is: " + largest);
    }
}
  1. I assume that the user enters twelve numbers separated by a space in a single line.
  2. Method split returns an array of size twelve where every element is one of the entered numbers.
  3. I initialize smallest to the largest int which guarantees that the first int that I compare it to is guaranteed to be smaller.
  4. Similarly for largest . I initialize it to the smallest possible int .
  5. You need a loop in order to populate all the elements of arr .

Here is output from a sample run of the above code.

Please enter 12 numbers: 1 2 3 4 5 6 7 8 9 0 -1 -2
The smallest value in the Array is: -2
The largest value in the Array is: 9

EDIT

Since you claim that JOptionPane is required, below code uses JOptionPane .

String line = JOptionPane.showInputDialog("Please enter 12 numbers: ");
String[] numbers = line.split(" ");
int k = 0;
int[][] arr = new int[3][4];
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        arr[i][j] = Integer.parseInt(numbers[k++]);
        if (arr[i][j] < smallest) {
            smallest = arr[i][j];
        }
        if (arr[i][j] > largest) {
            largest = arr[i][j];
        }
    }
}
JOptionPane.showMessageDialog(null, "The smallest value in the Array is:" + smallest);
JOptionPane.showMessageDialog(null, "The largest value in the Array is:" + largest);
System.exit(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM