I need to implement my own version of the StringTokenizer class. It would I'd like to know if I'm at least heading in the right direction with my isDelim and my first ST method, as they are the only ones I want to work with.
What I have tried:
Here are the 2 methods I want to work with:
public ST(String s, String d)
{
star = new String[50];
String str = s;
int start = 0;
int end =0;
String token;
numTokens = 0;
for(int i=1; i<str.length(); i++)
{
String c1 = str.substring(i, i+1);
String c2 = str.substring(i-1, i);
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 ++;
}
}
public boolean isDelim(String s)
{
String delim = s;
return delim.substring(0, 1).equals(" ");
}
And here's everything together:
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;
String token;
numTokens = 0;
for(int i=1; i<str.length(); i++)
{
String c1 = str.substring(i, i+1);
String c2 = str.substring(i-1, i);
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 ++;
}
}
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 Token
{
public static void main(String argv[])
{
String str;
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");
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");
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() + "]");
}
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());
}
}