Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.append() method

 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
I am trying to write this method that gets rid of all the whitespace and punctuation in a statement. I try to use the .append() method but when I go to compile I get a "Cannot find symbol method append(char)" error. Here is the code for my method. Can anyone see why I am getting this error? Thanks.
 
Patel Chintan
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you are using 'outstring' variable of String class, and String class doesn't have append method. You should use StringBuilder or StringBuffer class.


 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String has concat not append method
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok well when I try to use the StringBuffer class I get a bunch of incompatible type errors, and when i try to use the .concat method it gives me an error saying that I cannot use that method to add a char to a string... Something so simple seems so confusing right now.... Here is my entire code.. It might make it easier to spot what is going on... Thanks for the help..
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK you should read API

concat method takes only String not char.

so use concat(Character.toString(c)) and you should be all set
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That seems to have worked... Thank you.. But now it is saying that I may not have initialized the variable outstring... Any idea whats going on with that?
 
Bryan Peach
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok forget that last post... stupid me.. i figured it out... but now when i compile i get this error message.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at Palindrome.isPal(Palindrome.java:47)
at Palindrome.main(Palindrome.java:78)

Any ideas?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are declaring variable inside a method or block it is called local variable.

All the instance or class level variables are initialized to default value but not the local variable not even to null if that local variable is reference type.

So you should initialize it befire you use that variable.

So it should be String outString=null;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic