80 likes | 167 Views
Important Ref. Data Types. Tokenizers String customers=“tom,dick,harry”; csv-> String s[]. What is a String Tokenizer?. Class that uses a string to create substrings Substrings are tokens. String input =“this is a sample string”; //– Garbage in
E N D
Important Ref. Data Types Tokenizers String customers=“tom,dick,harry”; csv-> String s[]
What is a String Tokenizer? • Class that uses a string to create substrings • Substrings are tokens. • String input =“this is a sample string”; //– Garbage in • String output[] = {“this”, “is”, “a”, “sample”, “string”}; // - Garbage out! • The output is a tokenized version of the input.
Where is StringTokenizer? • in the package java.util. • import java.util.StringTokenizer;
How do you make a StringTokenizer? StringTokenizer st = new StringTokenizer(“1,2,3,4 5\” –6”, “,\” ”);
How do you find out how many tokens there are? • int tn = st.countTokens();
How can I use the StringTokenizer? while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } String s = st.nextToken(); for (int i=0; s != null; s = st.nextToken()) { System.out.println(s); }
How do I read a CSV File? firstName, LastName, StAddress, City, State, ZIP, ph1, ph2, ph3 One record per line. String fn = Futil.getFileName(); File f = new File(fn); FileInputStream fis = new FileInputStream(f); BufferedReader br = new BufferedReader(fis);
How do I read a CSV File?... while ((String s = br.readLine()) != null) { processALine(s); } fis.close();