Java gets the minimum value realization method of one-dimensional array _java

Source: Internet
Author: User
Tags integer numbers trim

Write a program that accepts a single line of data that the user enters in the text box. These data are integer numbers, separated by spaces, with no limit on the number of spaces. The data are segmented into a 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, that is, use the trim () function to filter the left and right spaces of the user input string, and if the result is an empty string, use the Showmessagedialog method of the Joptionpane class to prompt the user "Please enter the digital content". If the user enters Non-empty, use the Charat function to judge each character in the user's input string, if it is neither a number nor a space, prompt "Enter contains non-numeric content", and then use the SetText () function to empty the data in the user's input box. By validating, you create a one-dimensional array of strings whose elements are the contents of a space-delimited user input string. It then creates an integral one-dimensional array and opens up space equivalent to the length of the string array. The input is then entered as an integer array through the valueof () function of the integer class. Creates a minimum variable and initializes the first element of an integer array. Use a For loop to traverse the integer array to extract the smallest integer, and finally use the SetText () function to display the minimum value to the specified label.

The code is as follows:

Copy 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 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, with a space as the separator.") 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-digital content");
Textfield.settext ("");
Return
}
}
string[] Numstrs = Arraystr.split ("{1,}"); Split string
int[] Numarray = new Int[numstrs.length]; To create an integral type array
Convert input As 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 smallest integer
min = Numarray[j];
}
}
Lblnewlabel_1.settext ("The smallest number in the array is:" + min); Displays the minimum value to the specified label
}
}

The effect is as shown in the figure:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.