Search code examples
javastringlinked-listreturn-typetoupper

Return type error of linked list toUppercase method


I have a class called LString (a linked list class) that works with my other Node class. I have written a toUppercase() method that traverses the character sequence and converts lowercase to uppercase.

My issue is the return type, a problem I seem to have when coding a lot. I am unsure of how to return the required LString type, since even if I type return LString it recognizes it as a variable and gives the same incompatible types errors.

Here is the explicit error:

    LString.java:134: error: incompatible types
      return current;
             ^
  required: LString
  found:    Node

How do I return this required type of LString in my method?

I am pretty new to Java, and getting a grasp on return types has seemed troublesome for me when writing this. Let me know if I should post my whole class. If my question is a little unclear let me know as well, I would like to be concise with the users in this forum.

As requested here is more of my code that specifies the declarations I have made in both classes.

My Node class:

      public class Node{
       public char data;
       public Node next;

       //constructors from page 956
       public Node()
       {
          this('\0',null);  //'\0' is null char for java
       }

       public Node(char initialData, Node initialNext)
       {
          data = initialData;
          next = initialNext;
       }
}

And my LString class (only constructor and my toUppercase method I will list):

public class LString{

   private Node front = null;  //first val in list
   private Node back;   //last val in list
   private int size = 0;
   private int i;

   public LString(){
      //construct empty list
      Node LString = new Node();
      front = null;

   }
   public LString toUppercase(){
      Node current = front;
      while(current != null){
         current.data = Character.toUpperCase(current.data);
         current = current.next;
      }
      return front;
   }
}

If I need to provide anymore info let me know.


Solution

  • To return the required LString simply do:

    return this;
    

    Because LString is the class that contains the linked list's first node, and all the methods that modify the list should simply return it. Also notice that this line is doing nothing in the constructor, you can delete it:

    Node LString = new Node();