Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: finding the amt of words and chars in a give file

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default finding the amt of words and chars in a give file

    So my code compiles fine, but I get a run-time error
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:332)
    at Hw13.main(Hw13.java:36)
    import java.util.*;
    import java.io.*;
     
    class Main
    {
    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 ));
     
          StringTokenizer strTokenizer = new StringTokenizer(filename,".");
     
          int chars = 0;
          int words = 0;
          int charA = 0;
          int the = 0;
     
          while ( sc.hasNext())
          {
             String s = sc.next();
             words++;
          }
     
          while ( strTokenizer.hasMoreTokens())
          {
             if ( strTokenizer.nextToken().equalsIgnoreCase("the")) the++;
             if ( strTokenizer.nextToken().equalsIgnoreCase("a") ||
                  strTokenizer.nextToken().equalsIgnoreCase("an")) charA++;
             strTokenizer.nextToken(); chars++;
          }
          System.out.println("chars " + chars);
          System.out.println("words " + words);
     
       }
      }
    What's wrong with my code?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: finding the amt of words and chars in a give file

    Each time around the loop you read multiple tokens (3 or 4). If you only have 1 token it is read by the first if statement. Then there are no more tokens and when the second if statement tries to read a token the exception is thrown. Perhaps you should only read 1 token each time around the loop and store it in a variable.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: finding the amt of words and chars in a give file

    how would i go about putting the strings read into an array?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: finding the amt of words and chars in a give file

    Simple. Read a String, insert into array. Rinse and repeat.
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    mysticCHEEZE (November 3rd, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: finding the amt of words and chars in a give file

    Ok I've redone the code but I have another issue. How do I read more than one line?
    import java.io.*;
    import java.util.*;
    import java.util.StringTokenizer;
     
    class Main
    {
       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));
     
       }
    }

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: finding the amt of words and chars in a give file

    Depends upon what you are trying to do. One option:

    var1 = read line
    var2 = read line
    do stuff with var1 and var2
    repeat if needed.

    Note that you are going to run into problems if the amount of input is odd.
    Improving the world one idiot at a time!

Similar Threads

  1. for loop with chars
    By dragon40226 in forum Loops & Control Statements
    Replies: 4
    Last Post: September 25th, 2011, 06:50 PM
  2. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  3. finding String in a file
    By nasi in forum Java Theory & Questions
    Replies: 12
    Last Post: May 31st, 2010, 02:16 PM
  4. counting words of a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 6th, 2010, 04:40 PM
  5. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 10:11 AM