This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer & String ??

 
Muhammad Farooq
Ranch Hand
Posts: 356
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the following code give the compile error. Any comments ...
================================================================
class Q2
{
public static void main(String arg[])
{
StringBuffer s[]={"A","B"};
System.out.println(s[0]+","+s[1]);
}
}
================================================================
--Farooq
 
Ashish Jaiman
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cause U have to create StringBuffer there is no constructor as
StringBuffer s="A";
hence this would work
StringBuffer s[]={new StringBuffer("A"),new StringBuffer("B")};
System.out.println(s[0]+","+s[1]);
 
Roopa Bagur
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to use s[].append method to add data into the stringbuffer
Originally posted by Muhammad Farooq:
Why the following code give the compile error. Any comments ...
================================================================
class Q2
{
public static void main(String arg[])
{
StringBuffer s[]={"A","B"};
System.out.println(s[0]+","+s[1]);
}
}
================================================================
--Farooq

 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my addition Muhammad
Get your clue from the error message -
---------- javac ----------
Q2.java:8: Incompatible type for array. Can't convert java.lang.String to java.lang.StringBuffer.
StringBuffer s[]={"A","B"};
^
1 error
Normal Termination
Output completed (2 sec consumed).
A String object is implicitly made for string literals . You are trying to assign String objects to
an array of StringBuffer type . Besides their individual class hierarchies are such that they will not
support a conversion(narrowing/widening/casting etc.) aswell .
I don't think s[].append will add items as individual array elements .
Correct me if i am wrong .
Ashish
[This message has been edited by Ashish Hareet (edited July 25, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic