• Post Reply Bookmark Topic Watch Topic
  • New Topic

Concatenate all of the strings in an array  RSS feed

 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a method that will concate all the strings in this array called String[] s.



 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program seems all right to me. What kind of errors are you getting? You could also try to use a while loop. Like this for example:



But this will only work if all the elements of the array contain something
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried to get the output I get this error: <identifier> expected

I used this to get the output.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalona,
Everything you've posted so far is fine. Can you post the rest of the program?
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a lot of trouble calling the methods for countCharacter() method (which is suppose to count all characters)and concateLines() method (which is to put the array of string on one line). I have searched the internet and continue to search, but I can not understand I have to do in order to get it to work.


[ November 24, 2007: Message edited by: Kalona Ark ]
 
Kalona Ark
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to everyone that helped me. I finally got it.
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Olivier Legat:
Your program seems all right to me. What kind of errors are you getting? You could also try to use a while loop. Like this for example:



But this will only work if all the elements of the array contain something



That code didn't work at all for me.


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The operator != is undefined for the argument type(s) int, null

at Child.main(cTest.java:6)


I didn't think that method would work just because you might exceed array bounds but this problem stopped that from happening. Should that work?
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oppps I tried comparing nulls with int's. That was my mistake sorry.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of the "while(s[i] != null)" is to stop whenever there's nothing left in the array. That's why it doesn't work unless all the elements are full, it will stop when ever an element is empty.

E.g.

s[0]= "hi"
s[1]= "dude"
s[2]= null **in other words: empty**
s[3]= "bye"

It will concatenate "hi" and "dude" but will stop at s[2]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried that loop with no nulls in the array? I would expect something undesirable to happen.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Olivier Legat:]   The whole point of the "while(s[i] != null)" is to stop whenever there's nothing left in the array

I did not look to see what Stan is looking at, but I can figure out from his post what he is looking at. That way of looping through an array that you show was used before Java came along and now all arrays carry length information so you can do int end = s.length; I would tell you some more but you can take this and what Stan said and build another way of doing it. What do you think Stan is looking at in your code that would cause him to post ?

Also, I do not think the original poster really has it working yet. String result = ""; sets result and Strings cannot be later set with result = result + separator + s[i]; as Kalona Ark shows. StringBuffer or StringBuilder, along with several other ways of doing it will work. Along with considering what Stan is saying and figuring out an approach from that way.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Jordan:
String result = ""; sets result and Strings cannot be later set with result = result + separator + s[i]; as Kalona Ark shows.


Of course it can. String objects are immutable, but references to String objects are not and can be reassigned at will.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note about efficiency...

The "+" operator for Strings in Java is implemented behind the scenes by using StringBuilder (or StringBuffer in Java versions older than 5.0). The compiler converts the use of "+" with strings behind the scenes like this:

If you use the "+" operator on Strings in a loop, then what you are actually doing is creating a StringBuilder object at each iteration in the loop, that StringBuilder is initialised with the content of the string, then the other string is concatenated, and a new String object is created with the characters in the StringBuilder. So your program gets converted to something like this:

That's not very efficient - in each iteration, a new StringBuilder object has to be created, data from 'result' is copied into the StringBuilder, then 'separator' and 's[i]' are concatenated, and a new String object is created in which the contents of the StringBuilder are copied. So, while it's not apparent from your source code, Java is doing a lot of unnecessary creating of objects and copying of data.

Your program will be much more efficient when you manually use StringBuilder yourself:

Using the "+" operator on Strings in a loop is a common efficiency trap in Java programs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!