• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading an array of String into StringBuffer  RSS feed

 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I don't want to go through each element in a for loop and add it that way, so I tried append() using the whole array, but the result is unexpected.



The argument I am passing in is: HelloWorld ... let's start to rock!

And the output is: And we are testing here how to read whole command line into a String: [Ljava.lang.String;@584479b2

Any assistance would be appreciated on what I am doing wrong here.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use String Buffer. Use StringBuilder instead.
There is nothing wrong with that output from an array. If you look in the Java® Language Specification, you find the only overridden method in an array is clone(). So you are using the basic version of toString inherited from Object and [LMyClass@ab1234cd is the sort of thing you get printed from an array. Look at the second link and it tells you what the parts mean. In arrays you get the class name prefixed by [L for all reference types and probably a semicolon before the @ sign. If you want to print the array so you can understand it, try
System.out.println(Arrays.toString(myArray));
If you have more than one [] use deepToString instead.

Are you using Java8? If so try this
You will have to go through the System class and the Arrays class to check the names of methods; I am 99.9% sure there is a spelling error in that code snippet which will prevent the code from compiling.
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look up StringBuilder#append you find it is equivalent to appending String.valueOf(Object) and if you look up that valueOf method, you see it calls toString, so you get whatever the toString method returns (or “null”).
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use String Buffer. Use StringBuilder instead.


Ah, yes, I did actually use StringBuilder in my code, just put the StringBuffer in the title by error, which I will edit.

My reason for using StringBuilder was because it isn't Thread safe and so no additional checks are done on it when editing it, is this the same reason you suggest I use it or is there a different reason?

Campbell Ritchie wrote:
There is nothing wrong with that output from an array. . . . I am 99.9% sure there is a spelling error in that code snippet which will prevent the code from compiling.


Ah, thanks!
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
I removed most of the quote; quoting the whole of a post simply makes the thread longer without adding any value.
you should have no concurreency problems because the StringBuilder is a local variable and is therefore not “visible” to more than one thread at a time. Even if you had two threads calling that methods, there would be two StringBuilder objects each “visible” only to one thread.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell pointed out, with the Java 8 JDK you can use streaming to perform the operation succinctly...

 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all!
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Campbell Ritchie
Marshal
Posts: 56586
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote: . . .
Arrays.stream(args).forEach(builder::append);
. . .
Much neater than my solution, but if you call that code with
java StringArrayConvert Campbell Ritchie is a right nuisance
then System.out.println(builder) will read
CampbellRitchieisarightnuisance
because you are not adding delimiters between successive words.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!