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
No, you'll have to do your own string tokenization manually to use two space charactersuse 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
..nm[Edited on October 3, 2005 at 10:40 PM. Reason : .]
10/3/2005 10:39:43 PM
the real question is why are you using two consecutive blank spaces as a delimiter? Seems like a poor choice of a delimiterWhy 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
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
that doesnt work for single spaces not intended to be used as tokens... for example:
"how now brown cow"
10/3/2005 10:58:31 PM
but just check if the second token is an empty string.
10/3/2005 11:01:36 PM
then you'd have to recombine the current token, with a space, with the previous one if you've only got one space between wordsit can be done, but its a very poor way of coding
10/3/2005 11:05:59 PM
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
setup a string that contains double whitespaceString 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
regular expressions
10/4/2005 12:32:31 AM
use split, tokenizer sucks
10/4/2005 1:04:52 AM
Thanks for the responses. Basically im given a file set up something like this:column 1 column 2 column 3oneword oneword two wordsoneword oneword onewordoneword oneword twowordsetc etc etcI 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
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
read this and you should hopefully be able to figure it outhttp://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
int i = int j = 0loop 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
10/4/2005 3:13:02 AM
dude, whatever you're doing, it is probably simpler than processing XML.
10/4/2005 7:53:14 AM
you should actually really be using java.util.regexhttp://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/package-summary.html
10/4/2005 9:07:57 AM
^ 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