4 Replies Latest reply: Nov 18, 2007 4:35 PM by 807600 RSS

    calling a file and counting the number of words in it-please help!!

    807600
      /**
      * @(#)WordCounterTwo.java
      *
      * WordCounterTwo application
      *
      * @author
      * @version 1.00 2007/11/17
      */

      import java.util.Scanner;
      public class WordCounterTwo {

      public static void main(String[] args) {
           Scanner keyboard = new Scanner(System.in);
           String fileName;
           int countWords;
           System.out.println("Please enter the name of the file: ");
           fileName = keyboard.nextLine();
           System.out.println(countWords.lastIndexOf());
           
           
      }
      }
        • 1. Re: calling a file and counting the number of words in it-please help!!
          807600
          How is this non-question different from this one?
          http://forum.java.sun.com/thread.jspa?threadID=5237770
          • 2. Re: calling a file and counting the number of words in it-please help!!
            807600
            I am getting error message as follows:
            cannot find symbol constructor StringTokenizer() on line

            I am asking the user to enter the name of a file, and the output is supposed to display the number of words in the file that chosen. I'm not sure if I am going about this right way, and not sure why I am getting the erorr messages.
            /**
             * @(#)WordCounter.java
             *
             * WordCounter application
             *
             * @author 
             * @version 1.00 2007/11/17
             */
            import java.util.Scanner;
            import java.util.StringTokenizer; 
            public class WordCounter {
                
                public static void main(String[] args) {
                     
                     String sentence;
                     Scanner keyboard = new Scanner(System.in);
                     StringTokenizer words = new StringTokenizer();  //line 17
                     
                     int numberWords;
                     System.out.println("Please enter a sentence");
                     sentence = keyboard.nextLine();
                     sentence = words.nextToken();
                     
                     while (words.hasMoreTokens())
                     {
                          numberWords++;
                     }
                     System.out.println(numberWords);
                }
            }
            • 3. Re: calling a file and counting the number of words in it-please help!!
              807600
              StringTokenizer doesn't have any no-arg constructor. You have to create them with a String to be tokenized. Read the docs.

              That's what the error message is saying: it can't find any symbol (method names are symbols, as are a lot of other things) called "StringTokenizer" that references a constructor that doesn't take an argument.
              • 4. Re: calling a file and counting the number of words in it-please help!!
                807600
                Now I can see that you are trying to call a default constructor on the StringTokenizer class, I don't think it has one.
                What was wrong with my almost identical answer in your other identical thread an hour ago?
                The info is helpful - the snide remarks not so well. I assumed this forum was called New To Java because it was to help people learn to unerstand what they are doing wrong. I apparently was not anticipating the "experts" to be so on a pedestal so high that made it acceptable do be so condescending.
                It's behaviour like this that makes the "experts" get "snide". Keep it up and you'll get a nickname like "Reedie".