So, this code is supposed take 5 integers form the text fields written by the user and sort the integers written in the text field in ascending order.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class PushCounterPanelSorter extends JPanel
{
private JButton sort;
private JLabel label;
private JTextField textfield, textfield2, textfield3, textfield4, textfield5;
public PushCounterPanelSorter()
{
textfield = new JTextField(10);
textfield.setBounds(50, 100, 200, 30);
textfield2 = new JTextField(10);
textfield2.setBounds(50, 150, 200, 30);
textfield3 = new JTextField(10);
textfield3.setBounds(50, 200, 200, 30);
textfield4 = new JTextField(10);
textfield4.setBounds(50, 150, 200, 30);
textfield5 = new JTextField(10);
textfield5.setBounds(50, 150, 200, 30);
sort = new JButton("Sort");
label = new JLabel();
sort.addActionListener(new ButtonListener());
add(sort);
add(label);
add(textfield);
add(textfield2);
add(textfield3);
add(textfield4);
add(textfield5);
setBackground(Color.cyan);
setPreferredSize(new Dimension(300, 40));
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int[] numsort = new int[] {};
int num1 = Integer.parseInt(textfield.getText());
int num2 = Integer.parseInt(textfield2.getText());
int num3 = Integer.parseInt(textfield3.getText());
int num4 = Integer.parseInt(textfield4.getText());
int num5 = Integer.parseInt(textfield5.getText());
numsort[0] = num1;
numsort[1] = num2;
numsort[2] = num3;
numsort[3] = num4;
numsort[4] = num5;
Arrays.sort(numsort);
for(int i = 0; i<numsort.length-2; i++) {
System.out.print(numsort[i]);
}
label.setText("Sorted: " + numsort);
}
}
}
It throws index out bounds exception, but I am pretty sure there is more than that. Any help will be appreciated.