Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

testing a stringtokenizer

 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Possible example of this:

will print out
one
two
three
Linda
[ November 08, 2002: Message edited by: Linda Jones ]
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they seem usefull but i want the results to go into a hashtable
could you be kind and ajust my code please?
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic