Java gets the minimum implementation of a one dimensional array

  • 2020-04-01 02:56:30
  • OfStack

Write a program to accept the user in the text box input a single line of data. These data are integer Numbers separated by Spaces with an unlimited number of Spaces. The data is divided into one dimensional array and the minimum value is extracted from the array and displayed in the interface. The idea is to validate the user's input first by filtering the left and right Spaces of the user's input string with the trim() function, and then prompt the user "please enter numeric content" with the showMessageDialog method of the JOptionPane class if the result is an empty string. If the user's input is not null, the charAt function is used to judge each character in the user's input string. If it is neither a number nor a space, the charAt function prompts "input contains non-numeric content", and the setText() function is used to empty the data in the user's input box. If it passes the validation, a string one-dimensional array is created whose elements are the whitespace-delimited contents of the user's input string. Then create an integer one-dimensional array and give it a space equal to the length of a string array. The input is then converted to an Integer array by the valueOf () function of the Integer class. Creates the minimum number variable and initializes it as the first element of the integer array. The for loop is used to iterate through the integer array to extract the smallest integer, and finally the setText () function is used to display the minimum value into the specified label.

The code is as follows:


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

public class ArrayMinValue {

    private JFrame frame;
    private JTextField textField;
    JLabel lblNewLabel_1 = new JLabel();
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ArrayMinValue window = new ArrayMinValue();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public ArrayMinValue() {
        initialize();
    }

    
    private void initialize() {
        frame = new JFrame(" Gets the minimum value of a one-dimensional array ");
        frame.setBounds(100, 100, 450, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel(" Enter multiple integers in the text box, separated by Spaces. Such as: 3 5 2 562 125");
        lblNewLabel.setBounds(10, 10, 414, 15);
        frame.getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(10, 35, 414, 21);
        frame.getContentPane().add(textField);
        textField.setColumns(10);       
        lblNewLabel_1.setBounds(115, 70, 309, 15);
        frame.getContentPane().add(lblNewLabel_1);
        JButton button = new JButton("u8BA1u7B97");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(10, 66, 93, 23);
        frame.getContentPane().add(button);     
    }
    protected void do_button_actionPerformed(ActionEvent e) {
        String arrayStr = textField.getText().trim();           //Remove left and right Spaces
        if(arrayStr.equals("")){
            JOptionPane.showMessageDialog(null, " Please enter numeric content ");
            return;
        }
        for (int i = 0; i < arrayStr.length(); i++) {                //Filtering illegal input
            char charAt = arrayStr.charAt(i);
            if (!Character.isDigit(charAt) && charAt != ' ') {
                JOptionPane.showMessageDialog(null, " The input contains non-numeric content ");
                textField.setText("");
                return;
            }
        }
        String[] numStrs = arrayStr.split(" {1,}");         //Split string
        int[] numArray = new int[numStrs.length];           //Create an integer array
        //Convert the input to an integer array
        for (int i = 0; i < numArray.length; i++) {
            numArray[i] = Integer.valueOf(numStrs[i]);
        }
        int min = numArray[0];                          //Create the minimum number variable
        for (int j = 0; j < numArray.length; j++) {
            if (min > numArray[j]) {                 //Extract minimum integer
                min = numArray[j];
            }
        }
        lblNewLabel_1.setText(" The smallest number in the array is: " + min);       //Displays the minimum value in the specified label
    }
}

The effect is shown in the figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201402/2014218173525572.png" >


Related articles: