3 Replies Latest reply: Mar 25, 2014 2:09 AM by twinklin_girl RSS

    Cannot find method append(java.lang.string) error

    twinklin_girl

      Hello, I am trying to run the following code but getting the error above.

       

      Please help.

       

      import java.io.*;

       

       

       

      public class StringBuilder {

          public StringBuilder() {

              super();

          }

         

          public static void main(String args[])

          {

                  // creates empty builder, capacity 16

                  StringBuilder sb = new StringBuilder();

                  // adds 9 character string at beginning

                  sb.append("Greetings");

          }

      }

        • 1. Re: Cannot find method append(java.lang.string) error
          PhHein

          Your class doesn't have a method append(String). You should not name your classes the same as API classes. Rename your class to MyStringBuilder or use the qualified name for java.lang.StringBuilder.

          • 2. Re: Cannot find method append(java.lang.string) error
            rp0428

            If you don't understand what PhHein is telling you then see if you undersand why this code gives you that same message:

            import java.io.*;

             

            public class abc {

                public abc() {

                    super();

                }

               

                public static void main(String args[])

                {

                        // creates empty builder, capacity 16

                        abc sb = new abc();

                        // adds 9 character string at beginning

                        sb.append("Greetings");

                }

            }

            Similar to what PhHein said: that class doesn't have a method append(String).

             

            Your code is equivalent to what I posted above. You named your method 'StringBuilder' so any reference to 'StringBuilder' is a reference to YOUR class, not the 'StringBuilder' class in Java (which would now have to be referenced as 'java.lang.StringBuilder'.

            • 3. Re: Cannot find method append(java.lang.string) error
              twinklin_girl

              Thanks guys .

               

              I have solved it . The following works ok :

              public class StringTest2 {

                  public StringTest2() {

                      super();

                  }

                 

                  public static void main(String args[])

                  {

                     // creates empty builder, capacity 16

                          StringBuilder sb = new StringBuilder();

                          // adds 9 character string at beginning

                          sb.append("Greetings");

                          System.out.println(sb);

                  }

               

              }