The java.util Package
The java.util package includes a number of utilities, including those for date and time functions, tokenizing strings, and generating random numbers. The java.util package also gives a number of data structures, or collection classes, that allow you to store and organize data efficiently.
java.util.StringTokenizer
The StringTokenizer class permits you to break up String objects into what are known as tokens. Tokens are separated by what we call delimiters. The StringTokenizer class is handy when you want to break apart a list of data items that are supposed to be in a specific format. For example, consider the following list of names:
Suzy, Yetti, John, Mary, Rover, Ezekiel
A comma followed by a space character separates each item in the list. Assume, if you want to parse only the names from the String and print them out. The following code constructs a StringTokenizer object to parse the names and print them to the console:
String names = "Suzy, Yetti, John, Mary, Rover, Ezekiel";
String delimiter = ", ";
StringTokenizer st = new StringTokenizer(names, delimiter);
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
The StringTokenizer constructor takes two String arguments: the String to tokenize and a list of delimiter characters. Notice that the delimiter String includes both the space character as well as the comma character. These characters do not want to be found sequentially in order to be delimited; all the tokenizer wants is to find at least one of the characters found in the delimiter String to work. Therefore, you would expect the program to print the following:
Suzy
Yetti
John
Mary
Rover
Ezekiel
java.util.Random
As you know, the Math random method creates a random number between 0.0 and 1.0. The first time this method is invoked, it generates a new java.util.Random object, with the current system time as its seed. Subsequent calls to the Math random method return the next generated number from the same Random object.
Use of the Math random method works well, for most cases. However, there are times when you will wish to have more control over the random values produced. For instance, when the Random class is seeded with the same value, it will generate the same output. You may want to specify a particular seed (a long value) so that you can control and monitor your results to make sure you get the same output each time. You may also want to provide specific objects their own Random object to make sure that each object gets its own unique distribution of random values, rather than having them all feed from the same Random instance.
Another advantage of using the Random class over the Math random method is that you can get values of different primitive types. The following code snippet demonstrates how you can get random values of type float, boolean, long, and double:
// create a new Random object with an initial seed of 200
Random random = new Random(200);
// print out the next random int value
System.out.println(random.nextInt());
// print out the next random int, restricting output to numbers between 0 and 20
System.out.println(random.nextInt(20));
// print out the next random boolean value
System.out.println(random.nextBoolean());
// print out the next random float value
System.out.println(random.nextFloat());
// print out the next random double value
System.out.println(random.nextDouble());
// change the generator's seed to 100
random.setSeed(100);
// print out the next random long value
System.out.println(random.nextLong());
The above program will always print the same set of values each time it is run. If you need different random results each time your program runs, just invoke the Random default constructor without any arguments. This will seed the generator with the current system time.
Since it is never the same time twice (at least within one millisecond of polling), this class will produce different results each time. Just in case you are curious, the above code will always produce the following results:
-1133938638
1
true
0.74504864
0.7007618335887928
-5128016860359238732


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks