aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String tokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String tokenizer" Watch "String tokenizer" New topic
Author

String tokenizer

Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 311

output
------
C:\jdk\bin\java.exe TokenTest
Working Directory - D:\ksoni\javaTests\
Class Path - .;c:\jdk\lib;c:\j2sdkee1.3\lib\j2ee.jar;c:\KawaEnt5.0\kawaclasses.zip;c:\jdk\lib\tools.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib\i18n.jar
1[1003]
2[$]
3[$]
4[03]
5[$]
6[$]
7[$]
8[$]
9[suggestion1]

1[1003]
2[03]
3[suggestion1]
Process Exit...
--------
question
--------
how do i get an array of tokens ?
i do not want the $ sign
the token may not be there !in that case array element should be null
and see the delim "$a" , it seems a is ignored !
what if i have delim of two chars or more ??
--kalpesh


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Gidday!
In the case of your delimiters, JAVA treats every character in your delimiter string as a separate delimiter. This means that '$' and 'a' are delimiters, but not "$a".
To get rid of the $-sign in your tokens, use the second one of your StringTokenizers, eg the one without true in the constructor. Here, true means that the delimiters are also returned as tokens (as you probably noticed)...
You can't have delimiters that are not chars.
If you want to store the tokens, consider whether you know for sure how many tokens the string will result in. If you do, use an ordinary array of String. If you don't, you might want to use a Vector or some other dynamic storage...
Hope this helps!
//Kaspar
Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 311
I could not know how to use String Tokenizer to get string array
i used good old indexof

--output--
C:\jdk\bin\java.exe StringToArray
Working Directory - D:\ksoni\javaTests\
Class Path - .;c:\jdk\lib;c:\j2sdkee1.3\lib\j2ee.jar;c:\KawaEnt5.0\kawaclasses.zip;c:\jdk\lib\tools.jar;c:\jdk\jre\lib\rt.jar;c:\jdk\jre\lib\i18n.jar;C:\jdk\lib
1 index[i-1] 4 index[i]5
2 index[i-1] 5 index[i]8
3 index[i-1] 8 index[i]9
4 index[i-1] 9 index[i]10
5 index[i-1] 10 index[i]11
6 index[i-1] 11 index[i]-1
7 index[i-1] -1 index[i]-1
8 index[i-1] -1 index[i]26
[1003]
null 2![]
[03]
null 2![]
null 2![]
null 2![]
[suggestion1 tp]
null ![null]
null ![null]

1003$$03$$$$suggestion1 tp
Process Exit...

------------------
"Sun Certified Java Programmer"
KS
"Failing to plan is like plannig to fail!"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String tokenizer
 
Similar Threads
how to avoid null pointer exception
Help
StringTokenizer
String tokenizer
how to cast array of Object