Results 1 to 2 of 2
  1. #1
    Kinney.j is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Need help with Array.sort()

    Hey guys. I am trying to implement the use of Arrays.sort() in a program. I am obviously doing it wrong, but I cannot figure out my issue. I need to write a program that allows a user to input 10 numbers, then that program needs to display them in ascending order. The part I am having problems with is getting the program to display them in ascending order. Here is what I have so far:

    Java Code:
    import java.util.Arrays;
    
    public class JamesKinneyhw3 
    {
    	public static void main(String[] args)
    	{
    		//Array variable declared and given 10 elements
    		double[] numList = new double[10];
    		
    		//Imported Scanner for user input
    		java.util.Scanner input = new java.util.Scanner(System.in);
    		
    		//Request user to enter set amount of numbers(size of array)
    		System.out.print("Enter " + numList.length + " numbers: ");
    		
    		//Loop that displays the 10 numbers entered
    		for (int i = 0; i < numList.length; i++)
    		{
    			numList[i] = input.nextDouble();
    			System.out.print(numList[i]+ " ");
    		}
    		
    		System.out.println();
    		//Displays the numbers entered in ascending order
    		System.out.println(sort(numList));
    	}
    	
    	//Method created to create the ascending number list
    	public static double[] sort(double[] numList)
    	{
    		Arrays.sort(numList);
    		System.out.println("Your numbers in ascending order are: ");
    		return numList;
    	}
    
    }

  2. #2
    Kinney.j is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help with Array.sort()

    Went through a bunch of other forum posts and found some stuff that my book/instructor hadn't informed me of and changed my code to this and it worked. Had no idea about Arrays.toString(). Thanks for viewing though. Sorry to waste your time.

    Java Code:
    import java.util.Arrays;
    import java.util.Scanner;
    public class JamesKinneyhw3 
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		//Array variable declared and given 10 elements
    		double[] numList = new double[10];
    		
    		System.out.println("Enter " + numList.length + " numbers:");
    		
    		//Loop that displays the 10 numbers entered
    		for (int i = 0; i < numList.length; i++)
    		{
    			numList[i] = input.nextDouble();
    			System.out.print(numList[i]+ " ");
    		}
    		
    		Arrays.sort(numList);
    		
    		System.out.println();
    		System.out.println();
    		System.out.println("Your numbers in ascending order are:");
    		System.out.println(Arrays.toString(numList));
    	}
    }

Similar Threads

  1. Trying to sort Array<int, String>
    By ddc in forum New To Java
    Replies: 7
    Last Post: 12-12-2010, 08:42 PM
  2. sort array >> need help
    By hongi in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 10:37 PM
  3. Need help. Array sort
    By buzz1500 in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 05:24 AM
  4. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 12:41 AM
  5. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:48 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
  •