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 int
s 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 int
s.
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
New contributor
add a comment |
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 int
s 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 int
s.
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
New contributor
add a comment |
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 int
s 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 int
s.
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
New contributor
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 int
s 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 int
s.
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
java beginner algorithm sorting swing
New contributor
New contributor
edited 53 mins ago
200_success
127k15149412
127k15149412
New contributor
asked 1 hour ago
Michael_Bay
61
61
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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