File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String buffer problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String buffer problems" Watch "String buffer problems" New topic

String buffer problems

Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
1 class Q2
2 {
3 public static void main(String arg[])
4 {
5 StringBuffer s[]={"A","B"};
6 System.out.println(s[0]+","+s[1]);
7 }
8 }
The answer was: Comile time error:Casting needed to convert String to StringBuffer in line no. 5.
Firstly what is StringBuffer and why is it giving me this error?
Thank You,
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
StringBuffer is a java API class.You can create mutable strings using this class.StringBuffer objects should be created using new operator.
StringBuffer buf = new StringBuffer("Javaranch");
In your code, you r trying to create in a wrong way.
Go thru the Java API specification for more details.

Swati Kale
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Swati is right!
When using stringBuffer in an array try this code

class Q2 {
public static void main(String arg[]) {
StringBuffer s[]={new StringBuffer( "A" ), new StringBuffer( "B" ) };
System.out.println( s[0].toString() + "," + s[1].toString() );

Hope this helps!
I agree. Here's the link:
subject: String buffer problems
It's not a secret anymore!