I am trying to sort a certain amount of integers. I have an error in my code. The code is below as well as my error message. Any suggestions of how to fix this? I believe it has to do with it being primitive data rather than an object?

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

    int[] intList;
    int size;

    Scanner scan = new Scanner(System.in);

    System.out.print("\nHow many integers do you want to sort? ");
    size = scan.nextInt();
    intList = new int[size];

    System.out.println("\nEnter the numbers...");
    for (int i = 0; i < size; i++)
        intList[i] = scan.nextInt();
    Sorting.insertionSort(intList);

    System.out.println("\nYour numbers in sorted order...");
    for (int i = 0; i < size; i++)
        System.out.print(intList[i] + " ");
    System.out.println();
    `
    and the error code is this.....


xception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Sorting.insertionSort
at Numbers.main(Numbers.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 51 seconds)'

Dani AI

Generated

The error reported in the thread (as and suspected) is a compile-time symbol problem: the compiler/IDE cannot resolve the reference to Sorting.insertionSort. This is usually caused by one of the following—not by primitives vs objects by itself: the Sorting class file is missing or not compiled, the method signature does not match (or is non-static), package declarations/imports are wrong, the public class name and file name differ, or Sorting.java has its own compile errors that prevent it being seen. As pointed out, a program cannot run if it did not successfully compile.

Checks to perform (in order):

  • Confirm a Sorting.java file exists and contains the expected class name (case-sensitive) and that it compiles without errors.
  • If insertionSort was written as an instance method, call it on an instance or make it static. Signatures must match exactly: insertionSort(int[]) vs some other parameter type.
  • If Sorting is in a package, ensure both files declare the same package or import the class and that files live in the correct folder structure.
  • Use the IDE’s Problems/Errors view and run a full Clean & Build to force recompilation (NetBeans/Eclipse sometimes leave stale class files).
  • For a quick built-in option, java.util.Arrays.sort(int[]) sorts primitive arrays without any custom code.

Example: a simple static insertion sort that accepts a primitive int[]:

public class Sorting {
    public static void insertionSort(int[] a) {
        for (int i = 1; i < a.length; i++) {
            int key = a[i];
            int j = i - 1;
            while (j >= 0 && a[j] > key) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = key;
        }
    }
}

If polymorphic behaviour is required (as the thread title suggests), use objects and generics; for example a generic insertion sort for Comparable types (call with Integer[], String[], etc.):

public class Sorting {
    public static <T extends Comparable<? super T>> void insertionSort(T[] a) {
        for (int i = 1; i < a.length; i++) {
            T key = a[i];
            int j = i - 1;
            while (j >= 0 && a[j].compareTo(key) > 0) {
                a[j + 1] = a[j];
                j--;
            }
            a[j + 1] = key;
        }
    }
}

Following the checklist above should reveal the immediate cause; if compile errors remain, the IDE’s error output for Sorting.java will show the exact symbol problem that needs fixing.

Recommended Answers

All 4 Replies

How does the Sorting.insertionSort() method look like? Is it from any standard API?

Obviously you can't run the program if it didn't compile!
When you compiled the program it found an error and will have given you a detailed error mesage explaining what was wrong. What did that say?

As you have mentioned that

Sorting.insertionSort(intList)

where you have declared Sorting object,and you are calling insertionsort method with passing inList array argument..?

Show where you have make this method...

Your program shows at line 15-->

Sorting.insertionSort(intList); 

Can you please provide the details of this method "insertionSort" of class Sorting.
it is obvious from the error message that class "Sorting" is not there and hence current program could not be compiled.
Please write the a class "Sorting" with a static method "insertionSort(int[] numberList)".
Then try running the this code again.

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.