I'm running into an issue with my code and I can't figure out what's wrong. Basically the project I'm working on wants me to use static methods to perform a few different tasks using arrays. What I'm stuck on now, specifically, is printing my array.
The first thing I had to do was ask the user for the size of their array, and then have them input the data. After, I'm supposed to take the minimum value and maximum value, and then swap them. I'm pretty sure my code it right, but when I call the method I'm running into an error. This is what I have so far and I keep getting an error on lines 42 (beginning of swap method), 66 and 69:
public class ArraysStaticMethods {
static int[] array;
static public int arraySize;
static public int max;
static public int min;
//will create an array with user's input
private static int[] readInputs(int arraySize){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the length of your array: ");
arraySize = keyboard.nextInt();
array = new int[arraySize];
for (int i = 0; i <= array.length - 1; i++) {
System.out.print("Enter an integer: ");
int num = keyboard.nextInt();
array[i] = num;
}
//to test
System.out.println("Your array before:");
System.out.println(Arrays.toString(array));
return array;
}
//finds index of max and min values and swaps them
public static int[] swap(int[] array){
max = array[0];
min = array[0];
int maxIndex = 0;
int minIndex = 0;
for(int index = 1; index < array.length; index++){
if (array[index] > max){
max = array[index];
maxIndex = index;
}
if (array[index]<min) {
min = array[index];
minIndex = index;
}
}
array[maxIndex] = min;
array[minIndex] = max;
System.out.println("Your array after:");
System.out.println(Arrays.toString(array));
return array;
}
public static void displayOutputs(){
readInputs(arraySize);
swap(array);
}
public static void main(String[] args) {
displayOutputs();
}
}