Brit
Brit

Reputation: 1

How to print the results of a return method without using static or any static methods in Java

I am currently new to Java and need help. So i am suppose to create three methods to my sorting code without using "static". The three methods I am suppose to use are myRandom, to create an array of random numbers, mySort, to sort the array from highest to lowest, and printArray, to print the array. The problem is I do not know how to print the array after it has been sorted. Does the print method not work? This is my code so far:

import java.util.Arrays;
import java.util.Random;

public class SortAnimate6
{ 
    public static void main(String args[])
    {
        int numbers[] = new int[10];

        System.out.println("The array original:");



        System.out.println("The array generated:");


    }

    public int [] myRandom (int[] numbers)
    {
       Random random = new Random();
       for(int i=0; i<numbers.length; i++)
           numbers[i] = random.nextInt(20); 
       return numbers;
    }

    public  void printArray(int[] list)
    {
         for (int counter = 0; counter < list.length; counter++)
             System.out.print(list[counter] + " ");
         System.out.println();
    }

    public int[] mySort (int[] numbers)
    {
        for(int i=0; i<numbers.length; i++)
        {
            for(int j=0; j<numbers.length; j++)
            {
                if(numbers[i] < numbers[j])
                {
                    int temp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = temp;
                }
            }
        }
        return numbers;
     }
}

Upvotes: 0

Views: 129

Answers (4)

user207421
user207421

Reputation: 310884

You can't call any of those methods without creating an instance of SortAnimate6 to call them on.

Upvotes: 0

M Sach
M Sach

Reputation: 34424

If you are not rigid on using 3 methods you mentioned then you should go for this simple code

convert array to list with

Arrays.asList(yourArray);

Now simple use collections.sort(resultList)

Lastly iterate on list to print each number with sysout . Thats it

Upvotes: 0

JeffreDodd
JeffreDodd

Reputation: 209

After reviewing your code it appears that you need to be calling the methods that you're wanting to use. For example, if you want to call my sort you will need to pass the int array into the method by typing

mySort(myPassedIntArrayVariable);

You can also use the return statement you're in to set new variables inside your Main method. For example you could store the sort in a new variable such as

int[] storageArray = mySort(myPassedIntArrayVariable);

Hope this helps. Comment if this does not help your issue.

Upvotes: 1

Pete B.
Pete B.

Reputation: 3276

The methods printArray and mySort are not static, so they belong to the class.

I will give you part of the answer....

SortAnimate6 me = new SortAnimate6();
System.out.println("The array original:");
me.printArray(numbers);

Hopefully that will give you some things to try, and keep going.

Upvotes: 0

Related Questions