5 Replies - 4151 Views - Last Post: 13 February 2013 - 10:33 AM Rate Topic: -----

#1 ShadwClone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-January 13

Working with StringTokenizer

Posted 11 February 2013 - 09:56 PM

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
//////////////////////////////////////////////////////////////////////////



Is This A Good Question/Topic? 0
  • +

Replies To: Working with StringTokenizer

#2 raghav.naganathan   User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 412
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: Working with StringTokenizer

Posted 12 February 2013 - 03:06 AM

Well, you need to have a constructor for your StringTokenizer class which takes 2 string values as arguments like this.
public StringTokenizer( String str, String)
{
   //some code 
}


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Working with StringTokenizer

Posted 12 February 2013 - 04:04 AM

Difficult to know how to advise since what you say your intentions are don't seem to be backed up by the code. StringTokenizer is a legacy class. You should use String.split instead
Was This Post Helpful? 1
  • +
  • -

#4 ShadwClone   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-January 13

Re: Working with StringTokenizer

Posted 12 February 2013 - 10:01 PM

View Postraghav.naganathan, on 12 February 2013 - 03:06 AM, said:

Well, you need to have a constructor for your StringTokenizer class which takes 2 string values as arguments like this.
public StringTokenizer( String str, String)
{
   //some code 
}


regards,
Raghav

My goal is to have everything written within the driver (main)

My code is working a lot better now.. I was able to fix the problem I was having..

The only problem now is that I keep getting unwanted junk as my output, but I have no idea where it's coming from.

Filename? lettertest.txt
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fmodern\fcharset0
Courier;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\ql\qnatural
\f0\fs24
\cf0
An
abc,
the
def
--
ghij.\
This
123
is
the
second
line.\
}
letters 274
words 22
average word length 12.45
a/an   1 frequency 0.045
the    2 frequency 0.091


Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan   User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 412
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: Working with StringTokenizer

Posted 12 February 2013 - 11:34 PM

Well, those are RTF tags...looks like you are not using a plain-text editor to save your file.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Working with StringTokenizer

Posted 13 February 2013 - 10:33 AM

StringTokenizer is legacy code and is there just to support old application with already written code that use it.
New developed code should use the String class split() method
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1