Sorting an array of numbers in Java with an algorithm











up vote
1
down vote

favorite












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();
}

});

}
}









share|improve this question









New contributor




Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    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();
    }

    });

    }
    }









    share|improve this question









    New contributor




    Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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();
      }

      });

      }
      }









      share|improve this question









      New contributor




      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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();
      }

      });

      }
      }






      java beginner algorithm sorting swing






      share|improve this question









      New contributor




      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 53 mins ago









      200_success

      127k15149412




      127k15149412






      New contributor




      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      Michael_Bay

      61




      61




      New contributor




      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Michael_Bay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Michael_Bay is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209397%2fsorting-an-array-of-numbers-in-java-with-an-algorithm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Michael_Bay is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Michael_Bay is a new contributor. Be nice, and check out our Code of Conduct.













          Michael_Bay is a new contributor. Be nice, and check out our Code of Conduct.












          Michael_Bay is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209397%2fsorting-an-array-of-numbers-in-java-with-an-algorithm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'