Results 1 to 1 of 1
- 03-21-2011, 02:14 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Creating a custom String Tokenizer
I have been given a lab assignment where I am to write my own StringTokenizer class. I am having some trouble understanding the logic of how this is done. As my loop goes through the string one character at a time, it is supposed to determine if the character it is looking at is a delimiter or not. If it is a not delimiter then it looks at the character before it and if that character is a delimiter then it has found the start of a token and saves the index in an 'int' variable. Vice versa for the 'end' variable. I have been working on this for over a week and cannot come up with how this is done. The main contains 4 separate strings that each use this class to tokenize the string. I havnt even gotten the first one to work where it only has one space as the delimiter. Any suggestions on how I might be wrong in my logic?
Java Code:class ST { String star[]; int numTokens; int index = 0; public ST(String s, String d) { star = new String[50]; String str = s; int start = 0; int end = 0; numTokens = 0; String token; //String delim = d; for (int i = 1; i < str.length(); i++) { String t = str.substring(i, i + 1); String u = str.substring(i - 1, i); if (end != 0) { if (!t.equals(d)) { if (u.equals(d)) { start = str.indexOf(t); } } } if (t.equals(d)) { if (!u.equals(d)) { end = str.indexOf(t); token = str.substring(start, end); star[index++] = token; numTokens++; } } // System.out.println(token); } } public boolean isDelim(String s) { String delim = s; return delim.substring(0, 1).equals(" "); } public ST(String s) { this(s, " "); } public int countTokens() { return (numTokens - index); } public boolean hasMoreTokens() { return (numTokens > index); } public String nextToken() { return (star[index++]); } } public class Lab5 { public static void main(String argv[]) { String str; //1) str = "Hello world"; ST stok= new ST(str); System.out.println(str); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: " + stok.nextToken()); } System.out.println("#tokens = " + stok.countTokens()); System.out.println("\n\n"); //2) str = " Hello world "; stok= new ST(str); System.out.println(str); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: " + stok.nextToken()); } System.out.println("#tokens = " + stok.countTokens()); System.out.println("\n\n"); //3) str = "root:x:0:0:root:/root:/bin/bash"; stok = new ST(str, ":"); System.out.println(str); System.out.println("username = " + stok.nextToken()); System.out.println("password = " + stok.nextToken()); System.out.println("userid = " + stok.nextToken()); System.out.println("groupid = " + stok.nextToken()); System.out.println("comment = " + stok.nextToken()); System.out.println("home dir = " + stok.nextToken()); System.out.println("shell = " + stok.nextToken()); System.out.println("\n\n"); //4) str = "Hello-world.It is!a nice day,today"; stok= new ST(str,"-.!, "); System.out.println(str); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: " + stok.nextToken()); } System.out.println("#tokens = " + stok.countTokens()); }Last edited by kgrant5116; 03-21-2011 at 02:16 PM.
Similar Threads
-
Need help with string tokenizer
By ShortIt in forum New To JavaReplies: 1Last Post: 02-18-2011, 07:04 PM -
String Tokenizer help
By GreenTea in forum New To JavaReplies: 4Last Post: 10-30-2010, 02:44 AM -
String Tokenizer
By sumaih in forum Java GamingReplies: 2Last Post: 08-21-2010, 03:23 PM -
String Tokenizer
By hussainian in forum Advanced JavaReplies: 1Last Post: 03-16-2010, 08:58 AM -
String Tokenizer
By viperlasson in forum New To JavaReplies: 1Last Post: 03-09-2010, 01:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks