This discussion is archived
1 2 Previous Next 16 Replies Latest reply: Jan 22, 2007 11:29 PM by 807599 RSS

method overloading/ string method

807599 Newbie
Currently Being Moderated
I'm creating a program that uses method overloading and the compare to method
public class PLString
{

     
     public String PLString(String s)
     {
          String translated = s.substring(1, s.length()) + s.substring(0,1) + "ay";
          return translated;
     }
     
     public int equals(String s, String original)
     {
          String translated = s.substring(1, s.length()) + s.substring(0,1) + "ay";
          int compare = translated.compareTo(original);
          return compare;
     }
     
     public int equals(PLString s, String internal)
     {
          int compare = s.compareTo(internal);
          return compare;
     }
          
     
     public String toString(String text)
     {
          return text;
     }
     
     public int compareTo(String s, String internal)
     {
          s = s.substring(1, s.length()) + s.substring(0,1) + "ay";
          int compare = s.compareTo(internal);
          return compare;
     }
     
     public int compareTo(PLString s, String internal)
     {
          int compare = s.compareTo(internal);
          return compare;
     }
     
I get the following problems

cannot find symbol
symbol : method compareTo(java.lang.String)
location: class PLString
int compare = s.compareTo(internal);
^
cannot find symbol
symbol : method compareTo(java.lang.String)
location: class PLString
int compare = s.compareTo(internal);
^
  • 1. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    the problems are in my equals method, and my second compareto method
  • 2. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    Are you asking why you get those messages? If so, it's pretty much as the
    compiler says: you don't implement a method compareTo(java.lang.String) in
    the PLString class, yet you try and invoke such a method.
  • 3. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    how do I implement the method compareTo(java.lang.String) in
    the PLString class?
  • 4. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    You dont have a method with the signature
    int compareTo(java.lang.String)
    in your PLString class.
    but you try to invoke such a method inside your equals method and 2nd campareTo method.
    So just implement a method with that signature and try
  • 5. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    public class PLString
    {
         public int compareTo(java.lang.String)
         {
              
         }
         
    like that?
  • 6. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    Think this is what u want
    public int compareTo(String s){
      int compare = s.compareTo("anystring");
      return compare;
    }
  • 7. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    yes but i get the cannot find symbol method...
  • 8. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    did u implement that method in your PLString class?
    and still getting the same error?
  • 9. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    Thanks i got it to work

    now I want an instance data variable to be changed throughout each methods with out using the main method
    so i want the second 'text' variable to be the same as the first method
    public class test
    {
         public String text;
         
         public String PLString(String s)
         {
              String text = s.substring(1, s.length()) + s.substring(0,1) + "ay";
              return text;
         }
         
         public int equals(String s, String original)
         {
              //I want this text to be the text from the method PLString
              String text = s.substring(1, s.length()) + s.substring(0,1) + "ay";
              int compare = translated.compareTo(original);
              return compare;
         }
         
    so basically the text variable from the instatnce data variable will equal first method then second
  • 10. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    I don't see the use of this function:
         public int compareTo(PLString s, String internal)
         {
              int compare = s.compareTo(internal);
              return compare;
         }
    You have an instance of a PLString that you want to compare to a String. In order to do that you use a reference to another/the same PLString? You only need to pass a parameter String to compareTo. Doesn't your PLString class need to hold some value member to make t his class usefull? Ofherwise you can make everything static... I think you only need functions with signatures
         public int compareTo(String internal)
         {
              
         }
         public int compareTo(PLString internal)
         {
              
         }
    In that last post, the text instance variable isn't modified since you always create a new variable named text with local scope! You're hiding your instance variable!

    Message was edited by:
    Peetzore
  • 11. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    I don't see the use of this function:
         public int compareTo(PLString s, String internal)
         {
              int compare = s.compareTo(internal);
              return compare;
         }
    that one is just to compare two strings
  • 12. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    Shouldn't it be
    public int compareTo(String internal)
         {
               int compare = text.compareTo(internal);
               return compare;
          }
    then?
  • 13. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    yes i got all that fixed but now i want to have the text variable i created in the beginning be changed throughout each method...
    public String text;
         
         public String PLString(String s)
         {
              String text = s.substring(1, s.length()) + s.substring(0,1) + "ay";
              return text;
         }
         
         public int equals(String s, String original)
         {
              //I want this text to be the text from the method PLString
              String text;
              int compare = text1.compareTo(original);
              return compare;
         }
  • 14. Re: method overloading/ string method
    807599 Newbie
    Currently Being Moderated
    That's very basic use of Java classes and variables...
    class PLString {
         private String text;
         
         public PLString(String s)
         {
              text = s.substring(1, s.length()) + s.substring(0,1) + "ay";
         }
         
         public int equals(String original)
         {
              int compare = text.compareTo(original);
              return compare;
         }
    }
1 2 Previous Next