1

I am new to programming and Java as well. I need to write a void method which sorts the array entered, I have the code written but do not know how to display the sorted list from void method. Anyone willing to help. It will be greatly appreciated.

package util.assign4;

import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import util.IO;


public class UtilAssign4 {
    public static int[] getData (String input){

        StringTokenizer st = new StringTokenizer(input);
        int []x = new int[st.countTokens()];
        for(int i = 0;st.hasMoreTokens(); i++){
            try{
            x[i] = Integer.parseInt(st.nextToken());
            }
            catch(Exception e){ 
                JOptionPane.showMessageDialog(null," Invalid input");
                System.exit(1);
            }   
         }
        return x;    
    }
    public static int getHighest(int g[]){
        int  hi = g[0];
        for( int k = 1; k <g.length;k++)
            if(g[k]> hi) hi = g[k];

        return hi;
    }
    public static int getSmallest(int p[]){
        int sm = p[0];
        for(int l = 1;l<p.length;l++)
            if(p[l]  < sm) sm = p[l];
        return sm;
    }
    public static float getAverage(int n[]){
        float sum = 0.0f;
        for(int y = 0;y <n.length; y++) sum += n[y];
        return sum/n.length;

    }
    public static void getSorted(int grades []){
        for(int i = 0; i<grades.length-1;i++){
            int largest =i;
            for(int j = 0;j<grades.length;j++)
                if(grades[j]>grades[largest]) largest = j;
            int temp = grades[largest];
            grades[largest] = grades[i];
            grades[i]=temp;  

        }


    }


    public static void main(String[] args) {
       String input = JOptionPane.showInputDialog("Enetr one or more grades:");
       int [] x = getData(input);
       int j = getHighest(x);
       int m = getSmallest(x);
       float a = getAverage(x);



               IO.showMsg("Array you entered:" + input + String.format("\nThe"
                       + " higheset grade is:%2d \n"
                       + "The Lowest Grade is:%2d \n"+"The average is:%2.2f\n"
                       + "The sorted list: ",
                       j,m,a));


    }
}
Karki
  • 17
  • 1
  • 4

2 Answers2

2

You are not supposed to print the contents of the array in the sort method. Your requirement (I wager) is to sort the array supplied to the method 'in-place' (which it already looks like you are doing). What this means is that given an array:

int[] grades = new int[] {34, 76, 12, 0, -1};

That when you call:

UtilAssign4.getSorted(grades);

That the array passed into the method is actually sorted inside the method, and as such does not need to be returned (that's why your return type is void). So to summarize, before calling the sort method, your array is unsorted. After the call completes, tbe very same array has now been sorted.

So now you can then print out the sorted array in the calling method (in this case main(String[]):

getSorted(x); // <-- call the sort function, on your array

String msg = String.format("\nThe higheset grade is:%2d \n"
        + "The Lowest Grade is:%2d \nThe average is:%2.2f\n"
        + "The sorted list: %s", j, m, a, Arrays.toString(x));
IO.showMsg(msg);

Note the Arrays.toString(x)? That will take your sorted array, and convert it into a string representation (will look something like this: [76, 34, 12, 0, -1]).

Perception
  • 79,279
  • 19
  • 185
  • 195
0

in void method short your array in any field Array that is

 public class UtilAssign4 {
 private Integer[] shorted = new Integer[100];
 public static int[] getData (String input){
 .
 .

}

and do your stuff with above array in your void method and use this where you want

Pravin
  • 1,137
  • 12
  • 20
  • You are right, my method is supposed to be a void method, and I have to display the sorted list. Here is the my method: public static void getSorted(int grades []){ for(int i = 0; igrades[largest]) largest = j; int temp = grades[largest]; grades[largest] = grades[i]; grades[i]=temp; } How can I use this method to display the sorted list? – Karki Apr 04 '13 at 05:19