Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StringTokenizer in 1.6

7 views
Skip to first unread message

IchBin

unread,
Dec 21, 2006, 4:14:13 PM12/21/06
to
Just tried to run a program that uses StringTokenizer in JDK 1.6.0 but
it fails with NoSuchElementException and Unknown Source for nextToken().
I can get around it by using string.split().

Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at
weconsultants.org.JPear.JPearReaderTest.getTokens(JPearReaderTest.java:249)
at
weconsultants.org.JPear.JPearReaderTest.buildDataInstalledPackages(JPearReaderTest.java:218)
at
weconsultants.org.JPear.JPearReaderTest.processNetStat(JPearReaderTest.java:153)
at
weconsultants.org.JPear.JPearReaderTest.buildPanel(JPearReaderTest.java:119)
at weconsultants.org.JPear.JPearReaderTest.main(JPearReaderTest.java:103)


It was just unexpected. Has anyone have a problem with this?

Example

private static List getTokens(String inTargetLine)
{
List list = new LinkedList(); // Doubly-linked list
list = new ArrayList(); // List implemented as growable
array

StringTokenizer st = new StringTokenizer(inTargetLine);
while (st.hasMoreTokens())
{
list.add(st.nextToken());
}
return list;
}

--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

hiwa

unread,
Dec 22, 2006, 4:14:36 AM12/22/06
to
I get no error nor exception.
Your method just works fine for a space delimited string.

Oliver Wong

unread,
Dec 22, 2006, 10:14:12 AM12/22/06
to

"hiwa" <HGA0...@nifty.ne.jp> wrote in message
news:1166778876.3...@80g2000cwy.googlegroups.com...
[...]

> I get no error nor exception.
> Your method just works fine for a space delimited string.

IchBin, maybe you can post an SSCCE demonstrating the problem. Or at
least, give us the value for "inTargetLine" which causes the exception.
Maybe you're using some strange Unicode value which is confusing the
StringTokenizer (which has apparently been deprecated)?

- Oliver


IchBin

unread,
Dec 22, 2006, 1:56:29 PM12/22/06
to
Oliver Wong wrote:
> "hiwa" <HGA0...@nifty.ne.jp> wrote in message
> news:1166778876.3...@80g2000cwy.googlegroups.com...
>> IchBin wrote:
>>> Just tried to run a program that uses StringTokenizer in JDK 1.6.0 but
>>> it fails with NoSuchElementException and Unknown Source for nextToken().
>>> I can get around it by using string.split().
[snip code]

> [...]
>> I get no error nor exception.
>> Your method just works fine for a space delimited string.
>
> IchBin, maybe you can post an SSCCE demonstrating the problem. Or at
> least, give us the value for "inTargetLine" which causes the exception.
> Maybe you're using some strange Unicode value which is confusing the
> StringTokenizer (which has apparently been deprecated)?
>
> - Oliver
>
>

Thanks hiwa and Oliver. Yea, I remember about three year ago Sun
mentioned it wanted people to use string.split and get away from
StringTokenizer. Sorry, can't reason why I used StringTokenizer. I
rewrote to use string.split(). I am more concerned with my 1.6 install
if I am getting this error. I was not getting this error before. I
rebooted my machine and ran a test ala SSCCE. Here is the result:

<SSCCE>
public class problem
{
public static void main(String[] args)
{
List <String>list = new ArrayList<String>();
list = getTokens("1 22 3333 4444 55555");
}
private static List getTokens(String inTarget)
{
List <String>list = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(inTarget);
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
list.add(st.nextToken());
}
return list;
}
}
</SSCCE>

<output>
C:\Program Files\Java\jdk1.6.0\bin\java.exe -classpath "C:\Program
Files\Java\jdk1.6.0\lib\tools.jar;C:\Program
Files\Java\jdk1.6.0\lib\dt.jar;C:\Program
Files\Java\jre1.6.0\lib\rt.jar;C:\Documents and Settings\Ed Taylor\My
Documents\My Java Workarea" problem
1
3333
55555


Exception in thread "main" java.util.NoSuchElementException

at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at problem.getTokens(problem.java:19)
at problem.main(problem.java:10)
Finished executing
</output>

--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA

Message has been deleted

IchBin

unread,
Dec 22, 2006, 2:13:28 PM12/22/06
to
Oliver Wong wrote:
> "hiwa" <HGA0...@nifty.ne.jp> wrote in message
> news:1166778876.3...@80g2000cwy.googlegroups.com...
>> IchBin wrote:
>>> Just tried to run a program that uses StringTokenizer in JDK 1.6.0 but
>>> it fails with NoSuchElementException and Unknown Source for nextToken().
>>> I can get around it by using string.split().
[snip code]

>>>
> [...]
>> I get no error nor exception.
>> Your method just works fine for a space delimited string.
>
> IchBin, maybe you can post an SSCCE demonstrating the problem. Or at
> least, give us the value for "inTargetLine" which causes the exception.
> Maybe you're using some strange Unicode value which is confusing the
> StringTokenizer (which has apparently been deprecated)?
>
> - Oliver
>
>

Thanks hiwa and Oliver. Yea, I remember about three year ago Sun

mentioned it wanted people to use string.split and get away from

StringTokenizer. I changed to use string.split(). Sorry, can't reason
why I used StringTokenizer. Lapse in old habits I guess.

Today I was getting weird storage errors with the java vm under Eclipse.
So I rebooted my pc and all seems ok as I wipe the sweat off my brow..
lol. I was worried about my 1.6 install. Sorry and thanks for your help.

--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA

0 new messages