aspose file tools*
The moose likes Java in General and the fly likes HashSet.addAll Arrays.asList split  extra space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashSet.addAll Arrays.asList split  extra space" Watch "HashSet.addAll Arrays.asList split  extra space" New topic
Author

HashSet.addAll Arrays.asList split extra space

Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147


Looks , to me like a bug ?


Searchable nature photo gallery: http://agrozoo.net/jsp/Galery.jsp?l2=en
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What are you splitting on? The $ sign, or line end?
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147
The $ sign.
Point beeing:


-->
2$3
[1,2, 2, 3]
you see extra spaces before 2 and 3, which appears from nowhere. Hence I get "duplicate" 2

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Miran Cvenkel wrote:
[1,2, 2, 3]
you see extra spaces before 2 and 3, which appears from nowhere.

I can guarantee you, it does NOT "appear from nowhere". You also haven't shown us the contents of the original string before the split.

In fact, it might be better to show us a bit more of your code (including the split() call), along with the precise contents of the strings before and after splitting.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147
Here you have, test case that proves that something goes wrong in some cases.
Manifestation of that is that some numbers are getting in it with preceding spaces (WRONG), and some are not (OK)



See last out.print, how 13,33 are together and 44,55 pushed to right with space
Miran Cvenkel
Ranch Hand

Joined: Nov 23, 2010
Posts: 147
ahh damn, the secret of this:


is that 13,33 is all together one element. That is why I was confused.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Miran Cvenkel wrote:is that 13,33 is all together one element. That is why I was confused.

See how good it is when you have to explain a problem in full to someone else?

FYI, To ensure that your elements don't have any extra whitespace in them, you need to trim your input string first and then use something like
split("\\s*,\\s*")
('split on a comma surrounded by any amount of whitespace'). Viz:HIH

Winston
 
wood burning stoves
 
subject: HashSet.addAll Arrays.asList split extra space