File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes testing a stringtokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "testing a stringtokenizer" Watch "testing a stringtokenizer" New topic

testing a stringtokenizer

ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
hello anyone help please
what i need to do is if the text file does not have any text between two delimeters (comma) i want it to print out the empty space what should be between two delimeters i know it has to be in a if statement but how should it be?
could anyone give me some suggestions
Linda Jones
Ranch Hand

Joined: Aug 17, 2002
Posts: 57
Hi Ben,
When you create your StringTokenizer you need to use the constructor that will return your commas as well as the bits in between:
StringTokenizer(String str, String delim, boolean returnDelims) where returnDelims is true
Then if you get 2 commas returned in a row, you will know you need to put an empty space in.
Linda Jones
Ranch Hand

Joined: Aug 17, 2002
Posts: 57
Possible example of this:

will print out
[ November 08, 2002: Message edited by: Linda Jones ]
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
hello i need to get the text which is between the to tokens (comma) and put it into a hashtable but what i also want is if there is not any text between two commas in the file i want a blank space to be put into a hashtable so when i print it out it will display all the text and the blank spaces

import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;
public class ReadSource {

public static void main(String[] args) {

try {
boolean rt = false;
int ic = 1;
String Scounter = new String();
int icounter = 1;
Hashtable hash = new Hashtable(100);
FileReader file = new FileReader("Work.txt");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;
while (!eof){
s = new StringTokenizer(line,",",true);
while (s.hasMoreTokens()) {
String s2 = s.nextToken();
Scounter = String.valueOf(icounter);
if (s2.equals(","))
if (rt)
( s2.equals(""));

rt = true;
line = buff.readLine();
if (line == null)
eof = true;
for (Enumeration e = hash.elements();e.hasMoreElements() {
String se = (String)e.nextElement();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
}catch (NumberFormatException ne) {
System.out.println("Error - - " + ne.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You may want to see some of the past discussions of StringTokenizer:

"I'm not back." - Bill Harding, Twister
ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
they seem usefull but i want the results to go into a hashtable
could you be kind and ajust my code please?
I agree. Here's the link:
subject: testing a stringtokenizer
jQuery in Action, 3rd edition