Results 1 to 7 of 7
  1. #1
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Sorting Arrays in a New Class

    I need to have a program that reads the user's input, converts the input from a string to an int, then in a new class, sorts the input, and outputs the data. I have it working in one class, but I can't seem to have it sort in another class, then output it.

    Here is my working code:
    Java Code:
    import java.util.Arrays;
    import javax.swing.JOptionPane;
    
    public class SimpleSort {
    	public static void main(String[] args) {
    		String input = JOptionPane.showInputDialog("Enter a sequence of numbers separated by commas:");
    		String[] numbers = input.split(",");
    		int[] numArray = new int[numbers.length];
    		Arrays.sort(numbers);
    		
    		for(int i = 0; i < numbers.length; i++) {
    			numArray[i] = Integer.parseInt(numbers[i]);
    			
    			System.out.print(numArray[i] + ", ");
    		}
    	}
    }
    I tried bringing Arrays.sort() and the for() loop down into a another class:
    Java Code:
    class SortHelper {
    	puclic static int[] sort(int[] numbers) {
    		...
    	}
    }
    But all I get is an error. Any suggestions? Thanks for any help in advance.

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Any hope to see the error you are getting?

    Also, I dont think Arrays.sort() works with Strings. I know it works with ints.
    If you are parsing values from numbers[] into numArray[] why are you sorting numbers[] and not numArrays[]?
    Last edited by FlyNn; 08-28-2011 at 08:31 PM.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default

    I apologize, there's no error. Forgot to put a semicolon at the end of the statements. It works fine, and actually sorts the numbers when all kept in the same class, but when I add the second class either it doesn't sort, or nothing outputs. It's like nothing is being sent to or received from the second class.

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Could you show the code when you try to pass it to another class?
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default

    Here is what I am using:
    Java Code:
    import java.util.Arrays;
    import javax.swing.JOptionPane;
    
    public class SimpleSort {
    	public static void main(String[] args) {
    		String input = JOptionPane.showInputDialog("Enter a sequence of numbers separated by commas:");
    		String[] numbers = input.split(",");
    		int[] numArray = new int[numbers.length];
    		
    		for(int i = 0; i < numbers.length; i++) {
    			numArray[i] = Integer.parseInt(numbers[i]);
    			
    			System.out.print(numArray[i] + ", ");
    		}
    	}
    }
    
    class SortHelper {
    	public static int[] sort(int[] numbers) {
    		Arrays.sort(numbers);
    		
    		return numbers;
    	}
    }
    This only returns the numbers I entered in the order in which they were entered. If I was to leave it all in one class it works just fine. Another thing I just noticed is say I would enter: 2,5,12,6,9 as the numbers, I noticed that it would sort it as 12, 2, 5, 6, 9. Almost kind of an alpha-numeric sorting.

    By the way, thanks for the quicks responses, and again, thanks in advance for the help.

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Where in the main method are you calling the sort method from the SortHelper class?

    This is how I would do it:

    Java Code:
    public class SimpleSort {
    	public static void main(String[] args) {
    		String input = JOptionPane.showInputDialog("Enter a sequence of numbers separated by commas:");
    		String[] numbers = input.split(",");
    		int[] numArray = new int[numbers.length];
    		
    		for(int i = 0; i < numbers.length; i++) {
    			numArray[i] = Integer.parseInt(numbers[i]);
    		}
                    numArray = SortHelper.sort(numArray);
                    system.out.println(Arrays.toString(numArray));
    	}
    }
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    HSKrustofsky is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 03:19 AM
  2. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 09:35 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 02:00 AM
  5. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 03:18 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
  •