Results 1 to 8 of 8

Thread: java sort help

  1. #1
    dsexton18 is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default 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 donít have much programming experience

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    The array class has an overloaded static sort method.
    Arrays (Java 2 Platform SE v1.4.2)

    Example:
    Java Code:
    int[] a = {7,9,3};
    Arrays.sort(a);
    for (int i=0; i < a.length; i++)
         System.out.println(a[i]);
    Output:
    Java Code:
    3
    7
    9
    Last edited by Lil_Aziz1; 01-08-2010 at 04:13 AM.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    @Lil_Aziz1
    Arrays.toString will reduce your printing code to a one liner.

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    :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?

  5. #5
    dsexton18 is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    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 : ");

  6. #6
    dsexton18 is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    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]);

    }
    }

  7. #7
    dsexton18 is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    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]);

    }
    }

  8. #8
    dsexton18 is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  1. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM
  2. sort IP address in java
    By nilesh_123 in forum Networking
    Replies: 8
    Last Post: 10-18-2008, 11:08 PM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  4. Heap Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-16-2008, 11:27 PM
  5. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •