ncsubozo All American 541 Posts user info edit post |
Is there a way to have a double blank space as a delimiter for a string tokenizer?
StringTokenizer(String, " ") does not work.
Thanks,
Mike 10/3/2005 10:30:34 PM |
MiniMe_877 All American 4414 Posts user info edit post |
No, you'll have to do your own string tokenization manually to use two space characters
use the .indexOf methods to find the double spaces, and the .substring method to get the "token" from the string (these are both methods of the java.lang.String class) 10/3/2005 10:38:15 PM |
mattc All American 1172 Posts user info edit post |
..nm
[Edited on October 3, 2005 at 10:40 PM. Reason : .] 10/3/2005 10:39:43 PM |
MiniMe_877 All American 4414 Posts user info edit post |
the real question is why are you using two consecutive blank spaces as a delimiter? Seems like a poor choice of a delimiter
Why not use a tab '\t' or comma or pipe, unless of course you just have to deal with the double space for some worthwhile reason 10/3/2005 10:48:46 PM |
HaLo All American 14273 Posts user info edit post |
couldn't you just take every other token? wouldn't the 1st, 3rd, 5th etc... tokens include your strings while 2, 4, 6 etc would be empty strings? 10/3/2005 10:54:53 PM |
MiniMe_877 All American 4414 Posts user info edit post |
that doesnt work for single spaces not intended to be used as tokens...
for example: "how now brown cow" should come out as two tokens10/3/2005 10:58:31 PM |
HaLo All American 14273 Posts user info edit post |
but just check if the second token is an empty string. 10/3/2005 11:01:36 PM |
MiniMe_877 All American 4414 Posts user info edit post |
then you'd have to recombine the current token, with a space, with the previous one if you've only got one space between words
it can be done, but its a very poor way of coding 10/3/2005 11:05:59 PM |
HaLo All American 14273 Posts user info edit post |
actually you wouldn't since you could just add a space to the previous token. its not an elegant solution (making a different delimiter than two spaces is) but it would work. 10/3/2005 11:08:37 PM |
Dumbass All American 3412 Posts user info edit post |
setup a string that contains double whitespace String s = " "; then pass StringTokenizer(String, s);
... of course I'm too lazy to set up a test case and compile... just something worth trying
[Edited on October 3, 2005 at 11:56 PM. Reason : syntax] 10/3/2005 11:56:14 PM |
skokiaan All American 26447 Posts user info edit post |
regular expressions 10/4/2005 12:32:31 AM |
smoothcrim Universal Magnetic! 18968 Posts user info edit post |
use split, tokenizer sucks 10/4/2005 1:04:52 AM |
ncsubozo All American 541 Posts user info edit post |
Thanks for the responses. Basically im given a file set up something like this:
column 1 column 2 column 3 oneword oneword two words oneword oneword oneword oneword oneword twowords etc etc etc
I need to read in the file and have three strings per line. Since it seems to be a difficult solution, at least with my limited/forgotten java knowledge, im going to come back to it last
edit: there are 2 spaces between columns and one between words in the third column.
[Edited on October 4, 2005 at 1:10 AM. Reason : a] 10/4/2005 1:09:09 AM |
InsaneMan All American 22802 Posts user info edit post |
StringTokenizer weedTokenizer = new StringTokenizer("weed weed weed weed weed weed"," ",true);
weedTokenizer.nextToken() sometimes returns "weed" and other times returns " ". Check for spaces directly and count them.
I hate those damn weeds on my lawn. 10/4/2005 1:33:36 AM |
LittleZZ Veteran 442 Posts user info edit post |
read this and you should hopefully be able to figure it out http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String,%20int) 10/4/2005 2:30:44 AM |
bigben1024 All American 7167 Posts user info edit post |
int i = int j = 0 loop until EOF { if (charat(i) = 32 && charat(i+1) = 32){ if (arrayOfStrings[j].length > 0){ j++ } i+=2 }else { arrayOfStrings[j] += charat(i) //concatenate i++ } }//end loop
beautiful, no? This doesn't take into account the crlfs, but I think you can add that in if you need it.
[Edited on October 4, 2005 at 3:25 AM. Reason : make sense?]10/4/2005 3:13:02 AM |
philihp All American 8349 Posts user info edit post |
dude, whatever you're doing, it is probably simpler than processing XML. 10/4/2005 7:53:14 AM |
scud All American 10804 Posts user info edit post |
you should actually really be using java.util.regex
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/package-summary.html 10/4/2005 9:07:57 AM |
MiniMe_877 All American 4414 Posts user info edit post |
^ quite right, I always forget that Java can do regular expressions, but only because I'm still supporting java applications that run in the 1.3 JVM 10/4/2005 9:12:22 AM |