• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String buffer problems

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Kamil
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swati is right!
When using stringBuffer in an array try this code
<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() );
}
}

</CODE>
Hope this helps!
reply
    Bookmark Topic Watch Topic
  • New Topic