aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "testing a stringtokenizer" Watch "testing a stringtokenizer" New topic
Author

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
ben
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
Linda Jones
Ranch Hand

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

will print out
one
two
three
Linda
[ 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
thanks
ben

import java.io.*;
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(","))
hash.put(Scounter,s2);
if (rt)
( s2.equals(""));

else
rt = true;
//hash.put(Scounter,s2);
icounter++;
}
line = buff.readLine();
if (line == null)
eof = true;
}
for (Enumeration e = hash.elements();e.hasMoreElements() {
String se = (String)e.nextElement();
System.out.println(se);
}
buff.close();
}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
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You may want to see some of the past discussions of StringTokenizer:
http://www.coderanch.com/t/392278/java/java/StringTokenizer
http://www.coderanch.com/t/391315/java/java/String-Tokenizer


"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?
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: testing a stringtokenizer