user2320140
user2320140

Reputation: 11

Cannot Find Symbol - StringBuffer

I am pretty new to programming and could use a little help. I am learning how to use the StringBuffer class and I have written a simple code. However, I keep getting the error message "cannot find symbol" when I try to run the program. Any suggestions would be great! Thanks!

public class StringBuffer{ 
    public static void main(String[] args) 
    {
        StringBuffer sbuff = new StringBuffer();

        //one attempt to append - error
        sbuff.append("Jenna");

        //second attempt to append - error
        String phrase = new String("Jenna");
        sbuff.append(phrase);

        //attempts to call upon other methods - errors
        System.out.println("Your name backwards is: " + sbuff.reverse());
        System.out.println("The length of your name is: " +sbuff.length());
    }
}

The exact error looks like this:

cannot find symbol
  symbol:   method append(java.lang.String)
  location: variable sbuff of type StringBuffer

And similar errors for reverse and length.

Upvotes: 1

Views: 6806

Answers (2)

Bernhard Barker
Bernhard Barker

Reputation: 55609

There's a naming conflict. When you say StringBuffer sbuff = new StringBuffer(); this refers to your own class, not the one in the Java API, which I'm assuming you're trying to use.

You can fully qualify it:

java.lang.StringBuffer sbuff = new java.lang.StringBuffer();

But a better option would be renaming your class. As a general rule, don't name your class the same as a class you're trying to use (or the same as any class in the standard API for that matter), this will likely lead to confusion of yourself and probably anyone trying to read your code.

Also, you may want to use StringBuilder instead.

Upvotes: 3

Scott Shipp
Scott Shipp

Reputation: 2321

Do you have "import java.lang.StringBuffer;" in your code? Perhaps that's the reason. It would also help to know what error you receive?

Upvotes: 0

Related Questions