user1413110
user1413110

Reputation: 1

StringBuilder getting error using .append (Java)

I get the following errors:

nameCombine.java:18: error: cannot find symbol
sb.append(s);"
  ^

symbol:   method append(String)
location: variable sb of type StringBuilder
nameCombine.java:19: error: cannot find symbol
        sb.append("\t");
          ^
symbol:   method append(String)
location: variable sb of type StringBuilder
StringBuilder.java:16: error: cannot find symbol
        sb.append(s);
          ^
symbol:   method append(String)
location: variable sb of type StringBuilder
StringBuilder.java:17: error: cannot find symbol
        sb.append("\t");
          ^
symbol:   method append(String)
location: variable sb of type StringBuilder

After trying to compile the following code:

import java.util.ArrayList;

public class nameCombine {
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("firstName");
      list.add("middleName");
      list.add("lastName");

      StringBuilder sb = new StringBuilder();

      for (String s : list) {
         sb.append(s);
         sb.append("\t");
      }

      System.out.println(sb.toString());
   }
}

I'm sorry if this is a repeat question (I'm pretty sure it is) but a quick search didn't yield anything useful, for me anyways.

What im trying to do (if you can't already tell):
Combine an arrayList (firstName, middleName, lastName) into one new string. Any suggestions as to how I can do that in Java is appreciated.

Upvotes: 0

Views: 6609

Answers (3)

yuivc
yuivc

Reputation: 22

use StringBuffer instead of StringBuilder. Even I got the same error but it was resolved when I used StringBuffer.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Its Working perfectly fine, if you are using eclipse, try creating another this project again , sometimes eclipse do mess around...

I executed it as it is...and got the following output

firstName   middleName  lastName

Upvotes: 2

NPE
NPE

Reputation: 500893

There is absolutely nothing wrong with the code in your question.

The most likely explanation is that the code you're compiling isn't identical to the code that is in the question. For one thing, line numbers in the code and in the first two error messages don't agree with each other.

Upvotes: 2

Related Questions