user2974906
user2974906

Reputation: 3

How can I print a result from a void method

I'm doing an assignment for my intro to programming class and it's a bubble sort. The code may be flawed, but I'm not looking for someone to solve that problem. What my problem is, is that I'm trying to print it. I've been given the condition that the method had to be defined by a void method by the line "public static void sort(int[] array)". So, if I tried import Arrays, and used System.out.println(Arrays.toString(sort(array))); in my main method, it wouldn't work because I get a compiler error saying that void does not apply. If I tried to put it into a loop at the main method, it tells me that there are incompatible types. What is found is a void, what is required is an int[], but the original condition of the assignment is to use a void method. So, with that being said, should I change the method from void to int[] for testing purposes and submit the assignment as a void, or is there a way to print the output of this code with the void and I'm just missing it?

public static void sort(int[] array)
      { 
        int[] y = new int[array.length];
        for(int i = 0; i<=array.length-1; i++)
        {
          for(int j = i+1; i<=array.length-1; j++)
          {
            if(array[i] < array[j]){
              y[i] = array[j];
            }
            if(array[i] >= array[j]){
              y[i] = array[i]; 
          }  
        }
          for(int k = 0; k<y.length; k++){
          System.out.println(y[k]);
          }
      } 
    } //end of sort method

Upvotes: 0

Views: 9123

Answers (4)

A4L
A4L

Reputation: 17595

The problem is that you create an new local array inside your sort method which you obviously cannot return because of the restriction of you assignment.

int[] y = new int[array.length];

Your method will not be valid, since the array passed will remain unchanged. You need to do the sorting in place, i.e. without creating a new array. So that if you pass it from your main method, it gets sorted and you can print it there.

Bubble sort should use a swap method, so you just need one temporary variable.

Upvotes: 2

async
async

Reputation: 1537

In order to print the array, in your main method you'll need

//code that initializes the array or whatever
sort(myArray);
System.out.println(Arrays.toString(myArray)); //print the array modified by the previous method call

Also note what @A4L said about your local array. Work with the array you pass as parameter to your method, don't create a new one.

Upvotes: 1

Michael S
Michael S

Reputation: 1865

Since you have a void sort method, you want it to sort the array as a side effect. That is, modify the values in the int[] array reference you passed as a method parameter. Then you can print it from main.

Upvotes: 0

Tyler Lee
Tyler Lee

Reputation: 2785

Arrays.sort() modifies the array you pass in. So if you iterate over the array object and print the elements AFTER your call to Arrays.sort(), it will print for you.

Upvotes: 0

Related Questions