Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ST
- {
- private String inputString;
- private static String star[];
- private static int index;
- private static int numtokens;
- private static int start;
- private static int end;
- public ST(String str, String delim)
- {
- star = new String[50];
- String c1; String c2;
- String s;
- numtokens = 0;
- for(int i =0; i<str.length()-1;i++)
- {
- index = 0;
- s = str.substring(i,i+2);
- c1 = s.substring(0,1);
- c2 = s.substring(1);
- //System.out.println("[" + i + "]" + c1);
- //System.out.println("[" + i + "]" + c2);
- if(c1 !=(" ") && c2.equals(" "))
- {
- start = i +1;
- star[i] = str.substring(start);
- //System.out.println(" start = " + start);
- System.out.println("star[" + i + "] = "+ star[i]);
- numtokens++;
- }
- if(c1 != (" ") && c2 != (" "))
- {
- end= str.indexOf(' ',start);
- star[i] = str.substring(start, end);
- //j = star[i];
- //System.out.println("j = " + j);
- System.out.println("star[" + i + "] = "+ star[i]);
- numtokens++;
- }
- if(end>str.length() && start > end)
- {
- star[i] = str.substring(start);
- numtokens++;
- System.out.println("i" + i);
- }
- index =i;
- }
- }
- public static boolean isDelim(String c1, String delim)
- {
- for(int i = 0; i<delim.length()-1;i++)
- {
- if(c1.equals(" "))
- {
- return true;
- }
- }
- return false;
- }
- public ST(String str)
- {
- this(str," ");
- }
- public int countTokens()
- {
- return(numtokens-index);
- }
- public String nextToken()
- {
- return star[index++];
- }
- public boolean hasMoreTokens()
- {
- return(numtokens > index);
- }
- }
- public class Lab6
- {
- 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("token: " + stok.nextToken());
- System.out.println("\n\n");
- /*str = " Hello world ";
- stok= new StringTokenizer(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 StringTokenizer(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());
- //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 StringTokenizer(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());*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement