I am working on implementing my own version of a StringTokenizer class, this is
ID: 3600597 • Letter: I
Question
I am working on implementing my own version of a StringTokenizer class, this is what I've come up with so far. I've been working on it for a week, but stuck on what to do next. You are to implement your own version of the StringTokenizer class. Your program should function identically to the StringTokenizer class, but you are only required to implement the methods/constructors that are called from the test program (below). Your program should use the test program shown below, but should work for other sets of data as well. Do not modify my main method except for commenting out the sections of code which you have not (yet) implemented. NOTE: Your program can not use the StringTokenizer class or any other class or method that we have not covered in class. One simplifying assumption I made when I wrote my program is to assume that the Strings that are passed in will have no more than 50 tokens. Depending on how you write your program, this will make your code a tiny bit simpler. class ST { private String star[]; private int index = 0; private int numtokens =0; public ST(String s, String d) { star = new String[50]; String str = s; int start = 0; int end = 0; String delim = d; for(int i=1; i<str.length(); i++) { String c1 = str.substring(i, i+1); String c2 = str.substring(i-1, i); // System.out.println("[" + c1 + "]"); // System.out.println("[" + c2 + "]"); if(c1 == " ") { if(c2 != " ") { start = i+2; } } if(c1 != " ") { if(c2 == " ") { end = i; } } if(end < start) { end = str.length(); } star[numtokens] = str.substring(start,end); System.out.println("[" + star[numtokens] + "]"); numtokens++; } //loop across str, extract c1 & c2 // if c1 and c2 are "opposite" then // we have found start or end of token // if start of token, assign start variable // if end of token, assign end variable & extract token // assign to star array // increment numtokens } public boolean isDelim(String str) { String delim = str; return delim.substring(0, 1).equals(" "); } public ST(String str) { this(str," "); } public int countTokens() { return(numtokens - index); } public boolean hasMoreTokens() { return(numtokens > index); public String nextToken() { return star[index++]; } } public class Lab6 { 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("token: " + stok.nextToken()); System.out.println(" "); //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(" "); //3) str = "root:x:0:0:root:/root:/bin/bash"; stok = new ST(str, ":"); System.out.println("[" + str + "]"); int n = stok.countTokens(); System.out.println("#tokens = " + n); for (int i=0; i<n; i++) { System.out.println("token: [" + stok.nextToken() + "]"); } //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()); //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()); } } The output should look like this: = OUTPUT ============================================================== [Hello world] #tokens = 2 token: [Hello] #tokens = 1 token: [world] #tokens = 0 [ Hello world ] #tokens = 2 token: [Hello] #tokens = 1 token: [world] #tokens = 0 [root:x:0:0:root:/root:/bin/bash] #tokens = 7 token: [root] token: [x] token: [0] token: [0] token: [root] token: [/root] token: [/bin/bash] [Hello-world.It is!a nice day,today] #tokens = 8 token: [Hello] #tokens = 7 token: [world] #tokens = 6 token: [It] #tokens = 5 token: [is] #tokens = 4 token: [a] #tokens = 3 token: [nice] #tokens = 2 token: [day] #tokens = 1 token: [today] #tokens = 0
Explanation / Answer
Hi,
i have corrected your code and added comments wherever i changed, basically you are entering into tokens always irrespective of finding a token, i have corrected that, here is the code for the same.
public class Lab6
{
public static class ST
{
private String star[];
private int index = 0;
private int numtokens =0;
public ST(String s, String d)
{
star = new String[50];
String str = s;
int start = 0;
int end = 0;
//end=str.length();
String delim = d;
for(int i=1; i<str.length(); i++)
{
String c1 = str.substring(i, i+1);
String c2 = str.substring(i-1, i);
// System.out.println("[c1:" + c1 + " c2:"+c2+" i:"+i+"]");
// System.out.println("[start:" + start + " end:"+end+"]");
int flag=0;
if(c1.equals(" ") && !c2.equals(" "))
{
end=i;
//start = i+2;
flag=1;
}
if(c2.equals(" ") && !c1.equals(" "))
{
// end = i;
start = i;
flag=1;
}
if(end < start)
{
end = str.length();
}
if(flag==1)
{
// System.out.println("[start:" + start + "end: "+end+"]");
star[numtokens] = str.substring(start,end);
// System.out.println("[" + star[numtokens] + "]");
numtokens++;
}
}
//loop across str, extract c1 & c2
// if c1 and c2 are "opposite" then
// we have found start or end of token
// if start of token, assign start variable
// if end of token, assign end variable & extract token
// assign to star array
// increment numtokens
}
public boolean isDelim(String str)
{
String delim = str;
return delim.substring(0, 1).equals(" ");
}
public ST(String str)
{
this(str," ");
}
public int countTokens()
{
return(numtokens - index);
}
public boolean hasMoreTokens()
{
return(numtokens > index);
}
public String nextToken()
{
return star[index++];
}
}
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("token: " + stok.nextToken());
System.out.println(" ");
//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(" ");
//3)
str = "root:x:0:0:root:/root:/bin/bash";
stok = new ST(str, ":");
System.out.println("[" + str + "]");
int n = stok.countTokens();
System.out.println("#tokens = " + n);
for (int i=0; i<n; i++)
{
System.out.println("token: [" + stok.nextToken() + "]");
}
//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());
//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());
}
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.