• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

testing a stringtokenizer

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());

}
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to see some of the past discussions of StringTokenizer:
https://coderanch.com/t/392278/java/java/StringTokenizer
https://coderanch.com/t/391315/java/java/String-Tokenizer
 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic