Advertisement
J207739

Untitled

Mar 13th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. class ST
  2. {
  3.     private String inputString;
  4.     private static String star[];
  5.     private static  int index;
  6.     private static int numtokens;
  7.     private static int start;
  8.     private static int end;
  9.  
  10.  
  11.  
  12.     public ST(String str, String delim)
  13.     {
  14.        
  15.     star = new String[50];
  16.      
  17.     String c1; String c2;
  18.     String s;
  19.         numtokens = 0;
  20.  
  21.  
  22.     for(int i =0; i<str.length()-1;i++)
  23.     {
  24.         index = 0;
  25.        
  26.  
  27.         s = str.substring(i,i+2);
  28.         c1 = s.substring(0,1);
  29.         c2 = s.substring(1);
  30.  
  31.         //System.out.println("[" + i + "]"  + c1);
  32.         //System.out.println("[" + i + "]" +  c2);
  33.            
  34.             if(c1 !=(" ") && c2.equals(" "))
  35.             {
  36.                 start = i +1;
  37.                 star[i] = str.substring(start);
  38.                 //System.out.println(" start  = " +  start);
  39.          System.out.println("star[" + i + "] = "+ star[i]);
  40.                  numtokens++;            
  41.            }
  42.  
  43.               if(c1 != (" ") && c2 != (" "))
  44.           {
  45.  
  46.          end= str.indexOf(' ',start);
  47.          star[i] = str.substring(start, end);
  48.          //j = star[i];
  49.                  //System.out.println("j = " + j);
  50.          System.out.println("star[" + i + "] = "+ star[i]);
  51.          numtokens++;
  52.              }
  53.          if(end>str.length() && start > end)
  54.          {
  55.              star[i] = str.substring(start);
  56.          numtokens++;
  57.            
  58.                 System.out.println("i" + i);
  59.  
  60.  
  61.  
  62.              }          
  63.            
  64.              index =i;
  65.  
  66.            }
  67.      }
  68.  
  69.     public static boolean isDelim(String c1, String delim)
  70.     {
  71.         for(int i = 0; i<delim.length()-1;i++)
  72.     {
  73.         if(c1.equals(" "))
  74.          {
  75.           return true;
  76.              }
  77.  
  78.      }
  79.          return false;
  80.      }
  81.  
  82.  
  83.     public ST(String str)
  84.     {
  85.         this(str," ");
  86.     }
  87.  
  88.     public int countTokens()
  89.     {
  90.         return(numtokens-index);
  91.     }
  92.  
  93.     public String nextToken()
  94.     {
  95.         return star[index++];
  96.     }
  97.  
  98.     public boolean hasMoreTokens()
  99.     {
  100.         return(numtokens > index);
  101.     }
  102.  }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. public class Lab6
  113. {
  114.     public static void main(String argv[])
  115.     {
  116.         String str;
  117.  
  118.    
  119.         str = "Hello world";
  120.         ST stok= new ST(str);
  121.        
  122.         System.out.println("[" + str + "]");
  123.  
  124.         while (stok.hasMoreTokens())
  125.         {
  126.             System.out.println("#tokens = " + stok.countTokens());
  127.             System.out.println("token: " + stok.nextToken());
  128.         }
  129.         System.out.println("#tokens = " + stok.countTokens());
  130.         System.out.println("token: " + stok.nextToken());
  131.         System.out.println("\n\n");
  132.  
  133.    
  134.         /*str = "    Hello    world   ";
  135.         stok= new StringTokenizer(str);
  136.        
  137.         System.out.println(str);
  138.  
  139.         while (stok.hasMoreTokens())
  140.         {
  141.             System.out.println("#tokens = " + stok.countTokens());
  142.             System.out.println("token: " + stok.nextToken());
  143.         }
  144.         System.out.println("#tokens = " + stok.countTokens());
  145.         System.out.println("\n\n");
  146.  
  147.     //3)
  148.         str = "root:x:0:0:root:/root:/bin/bash";
  149.         stok = new StringTokenizer(str, ":");
  150.  
  151.         System.out.println(str);
  152.  
  153.  
  154.         int n = stok.countTokens();
  155.         System.out.println("#tokens = " + n);
  156.  
  157.         for (int i=0; i<n; i++)
  158.         {
  159.             System.out.println("token " + stok.nextToken());
  160.         }
  161.  
  162.         //System.out.println("username = " + stok.nextToken());
  163.         //System.out.println("password = " + stok.nextToken());
  164.         //System.out.println("userid   = " + stok.nextToken());
  165.         //System.out.println("groupid  = " + stok.nextToken());
  166.         //System.out.println("comment  = " + stok.nextToken());
  167.         //System.out.println("home dir = " + stok.nextToken());
  168.         //System.out.println("shell    = " + stok.nextToken());
  169.         //System.out.println("\n\n");
  170.  
  171.     //4)
  172.         str = "Hello-world.It is!a nice day,today";
  173.         stok= new StringTokenizer(str,"-.!, ");
  174.        
  175.         System.out.println(str);
  176.  
  177.         while (stok.hasMoreTokens())
  178.         {
  179.             System.out.println("#tokens = " + stok.countTokens());
  180.             System.out.println("token: " + stok.nextToken());
  181.         }
  182.         System.out.println("#tokens = " + stok.countTokens());*/
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement