Question

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.

Was it helpful?

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();

OTHER TIPS

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

front is of type Node, but the signature of the method is public LString toUppercase(), which means that it is expected to return an LString instance.

Think about what it is that you actually want to return. You want to return an LString that contains uppercase characters, right? But that is already the instance that you are working with! So you can either return this:

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

      return this;
}

But in this case you will still need another method that prints out the uppercase characters:

LString lString = new LString();
...
...
lString.toUppercase(); //lString is already modified and contains uppercase characters! You
                       //don't have to "return" anything. If you returned "this" this
                       //line would be lString = lString.toUppercase(), but that's
                       //not buying you anything special.
System.out.println(lString.toString()); //Assuming you have a toString method 
                                        //that prints out the characters.

By calling the toUppercase instance method you have already modified your LString instance so there is really no need to return anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top