aspose file tools*
The moose likes Java in General and the fly likes Effecient way to append line numbers to a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Effecient way to append line numbers to a String" Watch "Effecient way to append line numbers to a String" New topic
Author

Effecient way to append line numbers to a String

Alistair J MacDonald
Greenhorn

Joined: Jul 15, 2011
Posts: 8
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Jul 15, 2011
Posts: 8
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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Can't think of a more elegant way, but you might need to synchronise that method.

Anybody else?
Alistair J MacDonald
Greenhorn

Joined: Jul 15, 2011
Posts: 8
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
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
 
Don't get me started about those stupid light bulbs.
 
subject: Effecient way to append line numbers to a String
 
Similar Threads
Basic String function
Recursive sum of terms
Question about StringBuffer efficiency
Need help with read method
Checking whether a string is a substring of another string without using indexOf method