Write a program to accept a single line of data entered by the user in the text box. These data are all integer numbers, separated by spaces, and the number of spaces is not limited. And split the data into a one-dimensional array, and then extract the minimum value from the array and display it in the interface. The idea is to verify the user's input first, that is, first use the trim() function to filter the left and right spaces of the user's input string. If the result is an empty string, use the showMessageDialog method of the JOptionPane class to prompt the user "Please enter digital content." If the user input is not empty, use the charAt function to judge each character in the user input string. If it is neither a number nor a space, it will prompt "The input contains non-numeric content", and then use the setText() function to enter the user input box. The data in is cleared. If the verification is passed, a one-dimensional array of string type is created, whose elements are the contents of the user input string separated by spaces. Then create a one-dimensional array of integers and allocate space equal to the length of the string array. The input is then converted into an integer array through the valueOf() function of the Integer class. Create a minimum number variable and initialize it to the first element of the integer array. Use a for loop to traverse the integer array to extract the minimum integer, and finally use the setText() function to display the minimum value into the specified label.
The code is as follows:
Copy the code code 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();
/**
* Launch the application.
*/
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();
}
}
});
}
/**
* Create the application.
*/
public ArrayMinValue() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("Get 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("Please enter multiple integers in the text box, using spaces as delimiters. For example: 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("/u8BA1/u7B97");
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 digital content");
return;
}
for (int i = 0; i < arrayStr.length(); i++) { // Filter illegal input
char charAt = arrayStr.charAt(i);
if (!Character.isDigit(charAt) && charAt != ' ') {
JOptionPane.showMessageDialog(null, "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 input to integer array
for (int i = 0; i < numArray.length; i++) {
numArray[i] = Integer.valueOf(numStrs[i]);
}
int min = numArray[0]; // Create a minimum number variable
for (int j = 0; j < numArray.length; j++) {
if (min > numArray[j]) { //Extract the smallest integer
min = numArray[j];
}
}
lblNewLabel_1.setText("The smallest number in the array is: " + min); //Display the minimum value to the specified label
}
}
The effect is as shown in the figure: