Skip to main content
gamedev.net
🔒 Locked

[java] StringTokenizer issue

Started by 850t Sep 28, 2008 at 10:32 PM 2 replies 900+ views
Original Post
850t
850t
I have a question related to StringTokenizer. I'm using the code below to retrieve a line from a file. StringTokenizer token = new StringTokenizer(element, "*"); The line is as belowed. RMR*CR*00101539**0.01 I'm expecting the retrieved token.count to be 5 with an empty token between **. However, the output is 4 with token Token1 = RMR Token2 = CR Token3 = 00101539 Token4 = 0.01 Is there anything I can retrieve the empty token as well?
Japheth
Japheth
using element.split("*") should work.
INsanityDesign
INsanityDesign
The problem is, that the StringTokenizer does not deliver empty elements. There are some other Tokenizer implementation if you google for such. Otherwise iterate over the String yourself and attend the empty elements.
-----The next statement is not true!The previous statement is true!
Glass_Knife
Glass_Knife
Quote:
Original post by Japheth
using element.split("*") should work.


Yes and no. Yes, the split method will work, but for this example,
I get the following error...

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*


Because the example string uses the * quantifier, you should do this...

element.split("\\*");


Every time I am trying to split a file name with '.' I have the same problem.


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.