I am having error in the following code. What should I do to fix it? Also, how should I call it in main method to test the code? please HELP :(

package sortingelements;

public class Main {

    public static void main(String[] args) {
        
    }

    public int indexOfMaxInRange (int[] myArray, int begin, int end) {
        int index = -999;
        int maxInRange = -999;

        for (int i = begin; i < end; i++) {
            int temp = myArray[i];
            if (temp >= maxInRange) {
                index = i;
                maxInRange = temp;
            }
        }
        return index;
    }

    public void swapElement (int[] myArray, int first, int second) {
        int temp = myArray[first];
        myArray[first] = myArray[second];
        myArray[second] = temp;
    }

    public void sortArray (int[] myArray) {
        for (int i = 0; i < myArray.length; i++) {
            int index = indexOfMaxInRange (i, myArray.length);
            swapElement (myArray, index, i);
        }
    }
}

I am having error in the following code. What should I do to fix it? Also, how should I call it in main method to test the code? please HELP :(

package sortingelements;

public class Main {

    public static void main(String[] args) {
        
    }

    public int indexOfMaxInRange (int[] myArray, int begin, int end) {
        int index = -999;
        int maxInRange = -999;

        for (int i = begin; i < end; i++) {
            int temp = myArray[i];
            if (temp >= maxInRange) {
                index = i;
                maxInRange = temp;
            }
        }
        return index;
    }

    public void swapElement (int[] myArray, int first, int second) {
        int temp = myArray[first];
        myArray[first] = myArray[second];
        myArray[second] = temp;
    }

    public void sortArray (int[] myArray) {
        for (int i = 0; i < myArray.length; i++) {
            int index = indexOfMaxInRange (i, myArray.length);
            swapElement (myArray, index, i);
        }
    }
}

You must make all your methods static if you want to call them from main method of Main.class or create instance of Main in main()

For example:

public static void main( String[] args ) {
     Main main = new Main();
     main.sortArray( new int[] { 5, 2, 5, 1});
}

I am also having error on line 31. How should I fix it?

I am also having error on line 31. How should I fix it?

Please, describe details. Can you show exceptions stacktrace?

You probably should change line 31 to:

int index = indexOfMaxInRange (i, myArray.length - 1);

I guess you get an IndexOutOfBoundsException ;-)

You probably should change line 31 to:

int index = indexOfMaxInRange (i, myArray.length - 1);

I guess you get an IndexOutOfBoundsException ;-)

I tried this and there is still error. The error says required: int[],int,int Found: int,int

That is because your indexOfMaxInRange method takes:
(array, int, int)

where as you are passing it:
(int, int)

this is what it should look like.

int index = indexOfMaxInRange (myArray, i, myArray.length - 1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.