Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Working with StringTokenizer

$
0
0
Hello, I keep getting this error, but I have no idea why!

What I am attempting to do is read a file name from the user, then put it into one long string separated by a space (' '). Then as I go through I want to delete everything that is not a space or a letter.

StringTokenizer.java:20: cannot find symbol
symbol  : constructor StringTokenizer(java.lang.String,java.lang.String)
location: class StringTokenizer
      StringTokenizer strTokenizer = new StringTokenizer(str, " ,.");
                                     ^
StringTokenizer.java:21: cannot find symbol
symbol  : method hasMoreTokens()
location: class StringTokenizer
      while ( strTokenizer.hasMoreTokens())
                          ^
StringTokenizer.java:23: cannot find symbol
symbol  : method nextToken()
location: class StringTokenizer
         String s = strTokenizer.nextToken();
                                ^
3 errors


This is my code
import java.io.*;
import java.util.*;
///////////////////////////////////////////////////////////////////////////////
class StringTokenizer
{
//-----------------------------------------------------------------------------
public static void main (String [] args) throws Exception
{
Scanner kb = new Scanner ( System.in );
      System.out.print("Filename? ");
      String filename = kb.nextLine();
 
      Scanner sc = new Scanner ( new File ( filename ));
 
      int chars = 0;
      int words = 0;
      int charA = 0;
      int the = 0;
      String str = sc.nextLine();  
      StringTokenizer strTokenizer = new StringTokenizer(str, " ,.");
      while ( strTokenizer.hasMoreTokens())
      {
         String s = strTokenizer.nextToken();
         if ( s.equalsIgnoreCase("the")) the++;
         if ( s.equalsIgnoreCase("a") || s.equalsIgnoreCase("an")) charA++;
         words++;
         chars+=s.length();
         System.out.println(s);
      }
 
      System.out.println("chars " + chars);
      System.out.println("words " + words);
      System.out.printf("average word length %3.2f\n",(1.0*chars/words));
      System.out.printf("a/an  %2d frequency %4.3f\n", charA,(1.0*charA/words));
      System.out.printf("the   %2d frequency %4.3f", the, (1.0*the/words));
}
//-----------------------------------------------------------------------------
} // end class StringTokenizer
//////////////////////////////////////////////////////////////////////////


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>