Results 1 to 8 of 8
Thread: java sort help
- 01-07-2010, 08:52 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
java sort help
First of let me apologies if I posted in the wrong spot.
I am trying to figure out how to write a java program that will reads three integer inputs into variables. It must display inputs in both order entered and in sort order. This program should sort the numbers so that value1 <= value2 <= value3.
If some one can give me an idea where to start I would appreciate it. I dont have much programming experience
- 01-08-2010, 03:05 AM #2
The array class has an overloaded static sort method.
Arrays (Java 2 Platform SE v1.4.2)
Example:
Output:Java Code:int[] a = {7,9,3}; Arrays.sort(a); for (int i=0; i < a.length; i++) System.out.println(a[i]);
Java Code:3 7 9
Last edited by Lil_Aziz1; 01-08-2010 at 03:13 AM.
- 01-08-2010, 07:24 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
@Lil_Aziz1
Arrays.toString will reduce your printing code to a one liner.
- 01-08-2010, 10:49 PM #4
:O that is pretty cool. I thought only ArrayList and stuff did that. Although I don't see the static toString method in the array class in Arrays (Java 2 Platform SE v1.4.2) - and the toString method that the Array class inherits from the Object class returns the address of the array and it's not static.
I know it works because I tested it out myself but is it not documented?
- 01-09-2010, 09:09 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Thanks for the link that's a lot of information I haven't a clue where even to start . This is what I have so far. But I am unsure where to go from here. I read my text book twice it's of much help.
import javax.swing.JOptionPane;
public class Main{
public static void main(String [] args){
String num1= JOptionPane.showInputDialog("enter Integer : ");
String num2= JOptionPane.showInputDialog("enter Integer : ");
String num3= JOptionPane.showInputDialog("enter Integer : ");
- 01-09-2010, 10:32 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Ok I may be totally wrong but so far this is what I have came up with which is probably wrong.
import java.io.*;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class Test3{
public static void main(String [] args){
String num1= JOptionPane.showInputDialog("enter Integer : ");
String num2= JOptionPane.showInputDialog("enter Integer : ");
String num3= JOptionPane.showInputDialog("enter Integer : ");
String d = num1 = num2 = num3;
Arrays.sort(d);
for (int i=0; i < d.length; i++)
JOptionPane.showMessageDialog(d[i]);
}
}
- 01-10-2010, 03:57 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Ok I just about have it but I am trying to figure out how to make it dispaly the rsults in a dialog box insted of the console
import java.io.*;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class Sort2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String num1 = JOptionPane.showInputDialog("Enter Integer : ");
String num2 = JOptionPane.showInputDialog("Enter Integer : ");
String num3 = JOptionPane.showInputDialog("Enter Integer : ");
int intValue1 = Integer.parseInt(num1);
int intValue2 = Integer.parseInt(num2);
int intValue3 = Integer.parseInt(num3);
int [] a = {intValue1,intValue2,intValue3};
Arrays.sort(a);
for (int i=0; i < a.length; i++)
System.out.println(a[i]);
}
}
- 01-10-2010, 04:48 AM #8
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
I finally got it put was but was trying to figure out a way to display them on one dialog box then have it display the numbers as they where entered.
import java.io.*;
import java.util.Arrays;
import javax.swing.JOptionPane;
public class Sort2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String num1 = JOptionPane.showInputDialog("Enter Integer : ");
String num2 = JOptionPane.showInputDialog("Enter Integer : ");
String num3 = JOptionPane.showInputDialog("Enter Integer : ");
int intValue1 = Integer.parseInt(num1);
int intValue2 = Integer.parseInt(num2);
int intValue3 = Integer.parseInt(num3);
int [] a = {intValue1,intValue2,intValue3};
Arrays.sort(a);
for (int i=0; i < a.length; i++)
JOptionPane.showMessageDialog(null,a[i]);
}
Similar Threads
-
Using Merge Sort to sort an ArrayList of Strings
By coldfire in forum New To JavaReplies: 3Last Post: 03-13-2009, 01:03 AM -
sort IP address in java
By nilesh_123 in forum NetworkingReplies: 8Last Post: 10-18-2008, 10:08 PM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 08:04 PM -
Heap Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-16-2008, 10:27 PM -
Bubble Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks