Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Array -Sorting

  1. #1
    Junior Member
    Join Date
    Feb 2023
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Array -Sorting

    Write a program to sort a given array
    (Passing Array to a Method in Java- get an array from user input
    Returning Array from the Method-sort and then return the sorted array)
    import java.util.Scanner; 
    public class SortClass {
       public int Ascending(int [] myarray) {
           int n=0,temp;
           int a[] = new int[n];  
     
           for (int i = 0; i < n; i++)   
    {  
    for (int j = i + 1; j < n; j++)   
        {  
        if (a[i] < a[j])   
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;  
            }  
     
        }  
    System.out.println("Array elements in descending order:");  
    for ( i=n; i < n - 1; i--)   
        {  
            System.out.println(a[i]);  
        }  
    return (a[n - 1]);  
    }  
     
     
       }
     
     
    public static void main(String[] args)   
    {  
    int n, temp;  
    Scanner s = new Scanner(System.in); 
    SortClass SC=new SortClass();
    System.out.print("Enter the number of elements: ");  
    n = s.nextInt();  
    int a[] = new int[n];  
    System.out.println("Enter the elements of the array: ");  
    for (int i = 0; i < n; i++)   
    {  
    a[i] = s.nextInt();  
    }  
    for (int i = 0; i < n; i++)   
    {  
               SC.Ascending(a);
    }  
    System.out.println("Array elements in Ascending order:");  
    System.out.println("Array elements in Ascending order:"+SC.Ascending(a));  
     
    for (int i = 0; i < n; i++)   
    {  
    for (int j = i + 1; j < n; j++)   
    {  
    if (a[i] < a[j])   
    {  
    temp = a[i];  
    a[i] = a[j];  
    a[j] = temp;  
    }  
    }  
    }  
    System.out.println("Array elements in descending order:");  
    for (int i = 0; i < n - 1; i++)   
    {  
    System.out.println(a[i]);  
    }  
    System.out.print(a[n - 1]);  
    }  
     
    }

    Error

    --- exec-maven-plugin:3.0.0:exec (default-cli) @ mavenproject1 ---
    Enter the number of elements: 4
    Enter the elements of the array:
    50
    6
    70
    2
    Exception in thread "main" java.lang.RuntimeException: Uncompilable code - missing return statement
    at SortClass.Ascending(SortClass.java:1)
    at SortClass.main(SortClass.java:56)
    Command execution failed.

    Thanks in advance.
    Last edited by nandhutee; February 16th, 2023 at 09:01 PM. Reason: To Add Java Code

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array -Sorting

    What have you tried? Post your code if you need help with it.
    This site does not provide full source code.

    Be sure to wrap all posted code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2023
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array -Sorting

    k.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array -Sorting

    The code is very poorly formatted making it hard to read and understand. Please fix the code's formatting.
    missing return statement
    There is a missing return statement in the Ascending method (note java standards suggest method names should start with lowercase letter).
    There needs to be a return statement at the end of all execution paths. The return statement is inside of a loop which might not be executed. Make sure there is a return statement outside of the loop.
    The code's poor formatting makes it hard to see.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    nandhutee (February 18th, 2023)

Similar Threads

  1. Array sorting
    By renars in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2011, 10:45 AM
  2. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  3. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  4. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM