• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java string help please

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

}
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about StringTokenizer class?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I used boolean flag to see if it is the first company if it is other than first company I append it a :.

 
reply
    Bookmark Topic Watch Topic
  • New Topic