I'm working on an assignment for school and am stumped. I should mention that the operations of static declarations still elude me a bit...
import java.util.Scanner; public class SortedListArray implements SortedListInterface { public static void main(String[] args) { int i=0; int[] userArray = new int[i]; int noOfInputs = 0; int submission = 0; int n = 1; int inc = 0; Scanner kybd = new Scanner(System.in); System.out.println("Please enter the number of integers you will put into the array: "); noOfInputs = kybd.nextInt(); //holds the user-defined no. of integers to be held within the array for(i = 0; i < noOfInputs; i++) { kybd.nextLine(); System.out.println("Please enter integer" + n); submission = kybd.nextInt(); SortedListArray.process(userArray, submission, inc); n++; } System.out.println(SortedListArray.toString(userArray, noOfInputs)); SortedListArray.sort(userArray, noOfInputs); System.out.println ("Here are the newly sorted entries:\n"); System.out.println(SortedListArray.toString(userArray, noOfInputs)); } public static void sort(int[] intArray, int inputs) { // compares an index value at the beginning of the array to the next index value and switches their order // from smallest to largest in an ascending manner // this is continued for the entire array int temp; for(int i = 0; i < inputs; i++) { for(int j = i + 1; j < inputs; j++) if(intArray[i] > intArray[j]) { temp = intArray[i]; intArray[i] = intArray[j]; intArray[j] = temp; } } } public static String toString(int[] intArray, int inputs) { //returns a nicely formatted String value of all of the indices and the values contained within String stringFormat = null; System.out.println ("Current contents of the array are: \n\n"); for (int i = 0; i < inputs; i++) { stringFormat =(stringFormat + "Index " + i + ": " + intArray[i] + "/n"); } return stringFormat; } public static void process(int[] intArray, int submission, int num) { //processes the user input and moves them into the array indices intArray[num] = submission; num++; } }
And the implemented interface...
public interface SortedListInterface { public void sort(int[] intArray, int inputs); // Pre condition: Passed argument is an integer array // method reorganizes the contents of the array from smallest to largest public String toString (int[] intArray, int inputs); // pre conditions: Passed arguments must be a valid int array and int variable // returns a well-formatted print out of the current contents of the array public void process(int[] intArray, int inputs, int num); //pre conditions: Passed arguments must be a valid int array and int variable //processes the user input and moves them into the array indices }
So yeah, I get the error message that is shown in the title highlighted in all of the methods in the class file. If I take the static declaration away from the methods then the error will go away, but a new one pops up over any instance in which the method is called. It says, "Cannot make static reference to non-static method xxx"
Please school me on the meaning of static in regards to implementation. Or just in general.
Thanks!