Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – Exception in thread "main" java.lang.Error: Unresolved compilation problem

I am trying to implement a linked list using in Java, but I keep getting this error in my VSCode terminal.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method reverse() is undefined for the type SinglyLinkedList
I have two files, reverseSLL.java and removeDupesFromSLL.java. reverseSLL works perfectly fine if I delete the removeDupesFromSLL file from src folder. Can anyone please explain what I am doing wrong?
Here is the code reverseSLL.java:

class SinglyLinkedList{
    int data;
    SinglyLinkedList next;
    SinglyLinkedList head = null;

    public void add(int data){
        SinglyLinkedList newNode = new SinglyLinkedList();
        newNode.data = data;
        newNode.next = null;

        if(head == null){
            head = newNode;
        }
        else{
            SinglyLinkedList temp = head;
            while(temp.next != null){
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    public void print(){
        SinglyLinkedList temp = head;
        while(temp != null){
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }
    
    public void reverse(){
        SinglyLinkedList prev = null;
        SinglyLinkedList current = head;
        SinglyLinkedList next = null;
        while(current != null){
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        head = prev;
    }
}
public class reverseSLL {
    public static void main(String[]args){
        SinglyLinkedList sll = new SinglyLinkedList();
        sll.add(1);
        sll.add(2);
        sll.add(3);
        sll.add(4);
        sll.add(5);
        
        sll.print();    
        sll.reverse();
        sll.print();
    }
}

Code of RemoveDupesFromSLL.java:

class SinglyLinkedList{
    int data;
    SinglyLinkedList next;
    SinglyLinkedList head = null;

    public void add(int data){
        SinglyLinkedList newNode = new SinglyLinkedList();
        newNode.data = data;
        newNode.next = null;

        if(head == null){
            head = newNode;
        }
        else{
            SinglyLinkedList temp = head;
            while(temp.next != null){
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    public void print(){
        SinglyLinkedList temp = head;
        while(temp != null){
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public void sort(){
        SinglyLinkedList temp = head;
        while(temp != null){
            SinglyLinkedList temp2 = temp.next;
            while(temp2 != null){
                if(temp.data > temp2.data){
                    int tempData = temp.data;
                    temp.data = temp2.data;
                    temp2.data = tempData;
                }
                temp2 = temp2.next;
            }
            temp = temp.next;
        }
    }

    public void removeDuplicates(){
        SinglyLinkedList ptr = head;
        while(ptr.next != null){
            if(ptr.data == ptr.next.data) ptr.next = ptr.next.next;
            else ptr = ptr.next;
        }
    }
}

class removeDupesFromSLL{
    public static void main(String[]args){
        SinglyLinkedList sll = new SinglyLinkedList();
        sll.add(2);
        sll.add(3);
        sll.add(4);
        sll.add(1);
        sll.add(4);
        sll.add(5);
        sll.add(5);
        
        sll.print();    
        sll.sort();
        sll.print();
        sll.removeDuplicates();
        sll.print();
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The error is telling you exactly the problem:

The method reverse() is undefined for the type SinglyLinkedList

this means your class SinglyLinkedList doesnt have the method reverse().

The problem here is that SinglyLinkedList exists two times. You should rename them to be distinct and then the error will help you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading