Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

StringTokenizer in 1.6

Thread view: 
IchBin - 21 Dec 2006 21:14 GMT
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;
    }

Signature

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 - 22 Dec 2006 09:14 GMT
> Just tried to run a program that uses StringTokenizer in JDK 1.6.0 but
> it fails with NoSuchElementException and Unknown Source for nextToken().
[quoted text clipped - 36 lines]
> 'If there is one, Knowledge is the "Fountain of Youth"'
> -William E. Taylor, Regular Guy (1952-)
I get no error nor exception.
Your method just works fine for a space delimited string.
Oliver Wong - 22 Dec 2006 15:14 GMT
>> Just tried to run a program that uses StringTokenizer in JDK 1.6.0 but
>> it fails with NoSuchElementException and Unknown Source for nextToken().
[quoted text clipped - 29 lines]
>>          return list;
>>      }

[...]
> 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 - 22 Dec 2006 18:56 GMT
>>> 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.
[quoted text clipped - 6 lines]
>
>     - 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>

Signature

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

IchBin - 22 Dec 2006 19:13 GMT
>>> 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.
[quoted text clipped - 6 lines]
>
>     - 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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.