Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashSet.addAll Arrays.asList split extra space

 
Miran Cvenkel
Ranch Hand
Posts: 175
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Looks , to me like a bug ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you splitting on? The $ sign, or line end?
 
Miran Cvenkel
Ranch Hand
Posts: 175
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Miran Cvenkel
Ranch Hand
Posts: 175
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh damn, the secret of this:


is that 13,33 is all together one element. That is why I was confused.
 
Winston Gutkowski
Bartender
Pie
Posts: 9477
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic