• 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

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to test the stringtokenizer to see if the text between to comma delimeters is there or not ie if i read in this text (hello,how,,,are,you)from a file i want the output to be
hello
how
are
you
could anyone please adjust my code so it will woke please
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());

}
}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,
You could do something like the following (you'll likely need to tweak the code to fit your needs). I would recommend trying to keep things simple (no need to use Hashtables, etc.). There are many ways that this can be accomplished - the code below is just an example.
Also, I trust that you will understand the concepts behind the example code below and not just apply the answer directly in what you need to do...
-Biju
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic