| Author |
.append() method
|
Bryan Peach
Ranch Hand
Joined: Sep 08, 2009
Posts: 76
|
|
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
Joined: Mar 01, 2007
Posts: 80
|
|
Hi,
you are using 'outstring' variable of String class, and String class doesn't have append method. You should use StringBuilder or StringBuffer class.
|
Chintan Patel, SCJP1.5, http://datewithjava.blogspot.com
|
 |
Siva Masilamani
Ranch Hand
Joined: Sep 19, 2008
Posts: 377
|
|
|
String has concat not append method
|
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
|
 |
Bryan Peach
Ranch Hand
Joined: Sep 08, 2009
Posts: 76
|
|
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
Joined: Sep 19, 2008
Posts: 377
|
|
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
Joined: Sep 08, 2009
Posts: 76
|
|
|
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
Joined: Sep 08, 2009
Posts: 76
|
|
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
Joined: Sep 19, 2008
Posts: 377
|
|
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;
|
 |
 |
|
|
subject: .append() method
|
|
|