Using the StringTokenizer Class

Q: I was compiling a little program to run and check this class out when I got the error "StringTokenizer class not found." What did I do wrong?

A: Solution

Add this line above your class statement:

import java.util.StringTokenizer;

      or even:

import java.util.*;

   This allows Java to find the package containing the object StringTokenizer.

Factoids

   All objects, all of them, are first presumed to be found in the present working directory. If they are not a class you wrote, Java looks in:

java.lang.*

The java.lang package contains things link String, etc.

You can also use the fully qualified package name for an object:

String s1 = "This is one way";
java.lang.String s2 = "This is another";

So, if you don't want to place an import statement above your class, you can just:

java.util.StringTokenizer st = new java.util.StringTokenizer(strInput);

You do not have to import java.lang.*, but you can if you like.

Why the wacky package names? Well, this allows objects to have a namespace the prevents collisions. Thus, we can make our own String class, and it won't step on jav.lang.String, if and only if we take steps to make sure it's in a distinct package, and our references to String are unambiguous.