Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: cannot be resolved to a type

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default cannot be resolved to a type

    I have this code where I'm trying to implement the isPalindrome method using Stack but I get a "cannot be resolved to a type". What to understand from that and how to fix it?

    Here is my output
    [>>] Enter a string (or a ! to exit): csc
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
            LinkedStack cannot be resolved to a type
            The method push(char) is undefined for the type PalindromeChecker // also here, can you explain me this error? I thought that it was possible to push character into a stack 
     
            at assignment03PartB.PalindromeChecker.isPalindrome(PalindromeChecker.java:30)
            at assignment03PartB.PalindromeChecker.main(PalindromeChecker.java:55)

    Driver
    import java.util.Scanner;
     
    //
    // - Do not change the "main" method.
    // - Please ADD CODE to complete implementing the program
    //
    public class PalindromeChecker {
     
        private static boolean isPalindrome(String string) {
     
            char[] ch = string.toCharArray();
            for (char c : ch) {
                Sy stem.out.println(c);
            }
     
            StackInterface<Character> myStack = new LinkedStack<>(); // error come on this line 
            System.out.println("isEmpty() returns " + myStack.isEmpty());
     
            System.out.println("ch size : " + ch.length);
            for (int i = 0; i < ch.length; i++) {
                push(ch[i]);
            }
            System.out.println("Job done.");
            return true;
        }
     
        //
        // - Do not change the "main" method.
        // - Please ADD CODE to complete implementing the program
        //
        public static void main(String[] args) {
            //
            // - Do not change the "main" method.
            // - Please ADD CODE to complete implementing the program
            //
            Scanner input = new Scanner(System.in);
            System.out.print("[>>] Enter a string (or a ! to exit): ");
            String string = input.nextLine();
     
            while (!string.equals("!")) {
                if (isPalindrome(string)) {
                    System.out.println(" [+] Yes. \"" + string + "\" IS a palindrome!");
                } else {
                    System.out.println(" [-] No. \"" + string + "\" is NOT a palindrome!");
                }
                System.out.print("[>>] Enter a string: ");
                string = input.nextLine();
            }
     
            System.out.println("[<<] Thank you!");
            //
            // - Do not change the "main" method.
            // - Please ADD CODE to complete implementing the program
            //
        }
    }

    Stack Interface
    public interface StackInterface<T>
    {
       public void push(T newEntry);
       public T pop();
       public T peek();
       public boolean isEmpty();
       public void clear();
    }


    OurStack class implementing StackInterface
    public class OurStack<T> implements StackInterface<T> {
     
        public OurStack() {
            topNode = null;
        }
     
        @Override
        public void push(T newEntry) {
            topNode = new Node(newEntry, topNode);
        }
     
        @Override
        public T peek() {
            if (isEmpty()) {
                throw new EmptyStackException();
            } else {
                return topNode.getData();
            }
        }
     
        @Override
        public T pop() {
            T top = peek(); // Might throw EmptyStackException
            assert (topNode != null);
            topNode = topNode.getNextNode();
     
            return top;
        }
     
        @Override
        public boolean isEmpty() {
            return topNode == null;
        }
     
        @Override
        public void clear() {
            topNode = null;
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: cannot be resolved to a type

    LinkedStack cannot be resolved to a type
    Where is the declaration for the LinkedStack class?

    The method push(char) is undefined for the type PalindromeChecker
    Where is the push method declared in the PalindromeChecker class?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    siid14 (October 7th, 2022)

  4. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: cannot be resolved to a type

    I fixed the cannot resolve the error, thanks for the help on that point.
    I fixed it by writing : "StackInterface<Character> myStack = new OurStack<>();"
    With OurStack the class that implements StackInterface class

    The push method is inside the OurStack class that implements StackInterface. And I made myStack that is supposed to have this method implemented via the OurStack class right?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: cannot be resolved to a type

    Is the code working now?
    If not, please post the code and the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: cannot be resolved to a type

    From the original post, I only corrected the driver about the "cannot resolve error" that does not appear anymore now. So it is fixed.
    import java.util.Scanner;
     
    //
    // - Do not change the "main" method.
    // - Please ADD CODE to complete implementing the program
    //
    public class PalindromeChecker {
     
        private static boolean isPalindrome(String string) {
     
            char[] ch = string.toCharArray();
            for (char c : ch) {
                System.out.println(c);
            }
     
            StackInterface<Character> myStack = new OurStack<>(); // Line I fixed
            System.out.println("isEmpty() returns " + myStack.isEmpty());
     
            System.out.println("ch size : " + ch.length);
            for (int i = 0; i < ch.length; i++) {
                push(ch[I]); // line who has error
            }
            System.out.println("Job done.");
            return true;
        }
     
        //
        // - Do not change the "main" method.
        // - Please ADD CODE to complete implementing the program
        //
        public static void main(String[] args) {
            //
            // - Do not change the "main" method.
            // - Please ADD CODE to complete implementing the program
            //
            Scanner input = new Scanner(System.in);
            System.out.print("[>>] Enter a string (or a ! to exit): ");
            String string = input.nextLine();
     
            while (!string.equals("!")) {
                if (isPalindrome(string)) {
                    System.out.println(" [+] Yes. \"" + string + "\" IS a palindrome!");
                } else {
                    System.out.println(" [-] No. \"" + string + "\" is NOT a palindrome!");
                }
                System.out.print("[>>] Enter a string: ");
                string = input.nextLine();
            }
     
            System.out.println("[<<] Thank you!");
            //
            // - Do not change the "main" method.
            // - Please ADD CODE to complete implementing the program
            //
        }
    }

    Here is my output where the code doesn't work

    [>>] Enter a string (or a ! to exit): csc
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
            The method push(char) is undefined for the type PalindromeChecker
     
            at assignment03PartB.PalindromeChecker.isPalindrome(PalindromeChecker.java:35)
            at assignment03PartB.PalindromeChecker.main(PalindromeChecker.java:55)


    --- Update ---

    I would like to say that PalindromeChecker is the name of my file (PalindromeChecker.java) with my main method (driver) and the isPalindrome method I posted earlier.

    --- Update ---

    I think I fixed it. I called the push without specifying fom where.
    I just fixed it with myStack.push(ch[i]) and it work. I have others error but not related to the push anymore

    --- Update ---

    I have the following errors :

    [>>] Enter a string (or a ! to exit): csc
    c
    s
    c
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
            topNode cannot be resolved to a variable
            topNode cannot be resolved to a variable
            Node cannot be resolved to a type
            topNode cannot be resolved to a variable
            EmptyStackException cannot be resolved to a type
            topNode cannot be resolved
            topNode cannot be resolved to a variable
            topNode cannot be resolved to a variable
            topNode cannot be resolved
            topNode cannot be resolved to a variable
            topNode cannot be resolved to a variable
     
            at assignment03PartB.OurStack.<init>(OurStack.java:18)
            at assignment03PartB.PalindromeChecker.isPalindrome(PalindromeChecker.java:30)
            at assignment03PartB.PalindromeChecker.main(PalindromeChecker.java:55)

    I don't get those errors. I thought, my stack is correctly set up in terms of methods, etc... ?
    TopNode is supposed to be null when I create myStack and then when I push character in, it should create the stack right? I don't get that "topNode cannot be resolved to a variable" here

    --- Update ---

    I fixed it. I did understand that topNode variable wasn't set up in my ourStack class. Same for Node. So I set up the private variable topNode et Node class. It works now.
    Last edited by siid14; October 7th, 2022 at 07:45 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: cannot be resolved to a type

    It works now.
    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ClickListener cannot be resolved to a type
    By SamJava_the_Hut in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2019, 07:06 AM
  2. Object cannot be resolved to a type in JSP
    By archana.acharya in forum Java IDEs
    Replies: 1
    Last Post: September 4th, 2014, 10:49 PM
  3. Getting error as "cannot be resolved to a type"
    By Prince76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2013, 07:18 AM
  4. Error message "Type result cannot be resolved or is not a field"
    By asreall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 08:40 AM
  5. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 10:42 AM

Tags for this Thread