aspose file tools*
The moose likes Java in General and the fly likes Java string help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java string help please" Watch "Java string help please" New topic
Author

Java string help please

Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
Hi,
I need some help...
I have a java array list. while iterating through list I want to create a string of the element seperated by : How can I do that?

List arrayList = new ArrayList()

Iterator<String> iterator = null;
iterator = compName.iterator();
while (iterator.hasNext()) {
String name = iterator.next();

// Here the string I want to build shold be like IBM:HP:CISCO:Apple

}
Mathew Mintalm
Ranch Hand

Joined: Feb 21, 2010
Posts: 103

What about StringTokenizer class?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

AjooAli Dar wrote:Hi,
I need some help...
I have a java array list. while iterating through list I want to create a string of the element seperated by : How can I do that?



Okay, so what problem are you having? Have you never concatenated Strings before, with with += or with StringBuilder.append()?
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
Thanks, I believe I can use StringBuffer... But not sure How.... StringTokennizer will break the string. I have already those companies into array list and now I want to make one string... of it...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

AjooAli Dar wrote:Thanks, I believe I can use StringBuffer... But not sure How.


And? Did you read its docs? Did you google for examples? What did you try and where did you get stuck?
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30

Thanks I can use append. but i dont want : to be appended at the end.
company.append(partyName);
company.append(":");

I want
IBM:HP:CISCO

what I am getting IBM:HP:CISCO:


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

AjooAli Dar wrote:
Thanks I can use append. but i dont want : to be appended at the end.
company.append(partyName);
company.append(":");

I want
IBM:HP:CISCO

what I am getting IBM:HP:CISCO:


So, right now you're saying "add the company name, and then add the :", when what you really should be saying is, "add the company name, and then, if it's not the last one, add the :"

Got that so far?

Now, before you end up beating your head against the wall on that, I'll point out that it's generally easier to implement if you turn it around to, "if it's not the first one, add a :, and then add the company name". That is instead of adding a colon after every one but the last, add it before every one but the first.
Ajoo Bar
Ranch Hand

Joined: Mar 28, 2011
Posts: 30
Ok I used boolean flag to see if it is the first company if it is other than first company I append it a :.

 
jQuery in Action, 2nd edition
 
subject: Java string help please