• 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

Effecient way to append line numbers to a String

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I currently have a piece of code to append line numbers to a String, shown below, however I feel it is a bit over complex and messy and wondered if anyone knew of a more efficient way that this could be done. Specifically the formatWithNumbers method, the way that the strings are composed cannot be altered.

AtomicInteger is used as the method can be called by many threads.



The output produced must be:
1|This is the first line
2|This is the second line
3|This is the third line
4|This is the fourth line
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with it? Apart from your using \n for line end rather than the actual line end used by your computer? You can find out the exact property from this class, which shows all properties. Since the line.separator property always looks like this
it also prints it individual characters. Hint: \r = 0x000d and \n = 0x000a.You might do well to set a String constant LINE_SEPARATOR to the value of the line.separator Property and use that for inserting into your StringBuilder. You can make a regex to match all line end characters, or any combination of \n\r. You will find a list of line end characters in the Pattern class.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I earlier wrote:What is wrong with it? . . .

I meant that I could see little wrong with it. In fact I think it looks good. Apart from the use of \n, that is.
 
Alistair J MacDonald
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What is wrong with it?



Hi Campbell, thanks for the reply, I guess I just felt that it was a bit of a complicated method to do such a simple thing. And perhaps I was overlooking a more elegant way of doing this.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually because you haven't synchronized the method, it is conceivable that two threads can access the AtomicInteger simultaneously and you may end up with line numbers like 1| 3| 2| 4| Not sure on that point, however.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't think of a more elegant way, but you might need to synchronise that method.

Anybody else?
 
Alistair J MacDonald
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Actually because you haven't synchronized the method, it is conceivable that two threads can access the AtomicInteger simultaneously and you may end up with line numbers like 1| 3| 2| 4| Not sure on that point, however.



Yes agreed, although to be honest this is not important, it is only important that each line has a unique number at its start.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Crusading Chameleon likes the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic