1
\$\begingroup\$

This is pretty much one of my first programs that I have created in Java and I just wanted to ask if anyone sees some obvious errors or mistakes I made.

The purpose of this program is to sort numbers given by a user using an algorithm.

First off, the program gets an input in form of ints from the user, I have used a popup here, and than creates an array arr1 with that size. After that, another popup opens that asks the user for the 1st number he wants to sort. Then the 2nd, 3rd, etc, until the array has been filled with ints.

The array gets transferred into the sorting algorithm which, surprise, sorts the numbers into a different array arr2.

This array then gets passed into a method that takes a static frame and puts a text field with the numbers (in correct order) onto it. [+ 1 close and 1 repeat button]

If the repeat method (buttonRetry.addActionListener.actionPerformed) is called / the button is pressed, the frame gets "cleaned" (main_frame.getContentPane().removeAll();) and the main() method is called again.

Is it possible to make the process of this happening (getting input from the user and sorting that input) faster and do you have any additional advice about what and how I did this (Noob errors, graphical tips, etc.).

Here is the code :

import java.util.*;
import java.awt.*;
import java.awt.event.*; 
import javax.swing.*;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class AWTCounter extends JFrame {

    public static int[] arr1;
    public static JFrame frame = new JFrame("Super Simple Sorting Program");
    public static JFrame main_frame = new JFrame("Super Simple Sorting Program");

    public void main()
    {
        String input_1 = JOptionPane.showInputDialog(frame, "How many numers do you want to sort with SSSP ?");
            if (input_1 == null) {dispose(); System.exit(0);}
        int checksum = Integer.parseInt(input_1);

        arr1 = new int[checksum];

        int logNumber = 0;
        int debug = checksum;
        checksum = 0;

        while (checksum <= debug-1)
        {
            logNumber++;
            String input_2 = JOptionPane.showInputDialog(frame, "Please enter your " + logNumber + " . number.");
            int member = Integer.parseInt(input_2);
            arr1 [checksum] = member ;
            checksum++;
        }

        int[] arr2 = doSelect(arr1);

        print(arr2);
    }

    public int[] doSelect(int[] arr)
    {

        for (int i = 0; i < arr.length - 1; i++)
        {
            int index = i;
            for (int j = i + 1; j < arr.length; j++)
                if (arr[j] < arr[index]) 
                    index = j;

            int smallerNumber = arr[index];  
            arr[index] = arr[i];
            arr[i] = smallerNumber;
        }
        return arr;
    }

    public void print(int[] arr2)
    {
        JTextField t2;

        t2 = new JTextField("Here are your sorted numbers : " + Arrays.toString(arr2));
        main_frame.add(t2, BorderLayout.NORTH);

        JButton buttonEnd = new JButton("Close apllication");
        main_frame.add(buttonEnd, BorderLayout.CENTER);

        JButton buttonRetry = new JButton("Try again !");
        main_frame.add(buttonRetry, BorderLayout.SOUTH);

        main_frame.setLayout(new FlowLayout());
        main_frame.setSize(600,300);
        main_frame.setVisible(true);

        buttonEnd.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent event)
            {
                dispose();
                System.exit(0);
            }

        });

        buttonRetry.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent event)
            {
                main_frame.getContentPane().removeAll();
                main();
            }

        });

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.