Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String buffer problems

 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • 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
 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • 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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic