Results 1 to 10 of 10

Thread: Counting Words

  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default Counting Words

    Hi guys, how are you all. I'm trying to create a program that reads the characters from standard input counting how many words that contains and reports the number on its standard output. I have managed to create a program that counts the characters but am having trouble working with words.

    Here is my code so far: I'm sure there's an obvious mistake somewhere, i'm just a beginner.

    Java Code:
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    // Program to read characters on standard input, counting
    // how many words that contains and reports number on its
    // standard output.
    
    public class WordCount
    {
      public static void main(String [] args)
      {
        // There are 65535 different character values (2bytes)
        // The array will already be set to zero in each element.
        
        int[] wordCount = new int[65535];
        
        // we will read the input as characters.
        InputStreamReader input = new InputStreamReader(System.in);
        
        // the number of words found so far.
        int allWordsCount = 0;
        
        try
        {
          int currentWord;
          while ((currentWord = input.read()) != -1)
          {
            allWordsCount++;
    	wordCount[currentWord]++;
          }
        }
        
        catch (IOException exception)
        {
          System.err.println(exception);
        }
        
        finally
        {
          try { input.close(); }
          catch (IOException exception)
            { System.err.println("could not close input " + exception); }
        }
        
        System.out.println("the number of words read was " + allWordsCount);
        
      }
    }
    Any suggestions or assistance would be most appreciated :)

    Kind regards

    Shyam

  2. #2
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    What I understand from your question is that you want to count
    1. number of characters
    2. number of words.

    The first one is already done in your code.

    Java Code:
          int currentWord;
          while ((currentWord = input.read()) != -1)
          {
            allWordsCount++; [COLOR="YellowGreen"][B][I]//this is actually the count of characters eventhough you named it wrong[/I][/B][/COLOR]
    	wordCount[currentWord]++;
          }

    Now the number of words. Isn't it easier by counting number of SPACEs. No. of words = No. of spaces + 1 ;)

    And what was this for
    Java Code:
    int[] wordCount = new int[65535];
    Doesn't make any sense.

  3. #3
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    So would i use a while loop for that too...

    What can i set the variables to? e.g. I would need to create a No of spaces variable and a noOfWords. But how would i firstly count the number of spaces


    Regards

    Shyam
    Last edited by Shyamz1; 03-02-2011 at 03:11 PM.

  4. #4
    Skiller is offline Member
    Join Date
    Jan 2011
    Posts
    67
    Rep Power
    0

    Default

    Assuming words are separated by just a single space then most of the time the number of words is the number of spaces (the space character " ") + 1.

  5. #5
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    How would you set a variable to a space character. noOfSpaces = " "; doesn't work. is there a special way.

    Would you use the Character.isWhiteSpace(c)?

    Regards

  6. #6
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    A little more matured way of counting words is shown below. Doesn't require any extra loop. Everything can be done in your own loop.

    Java Code:
    	    	boolean wasWhiteSpace = false;
    	    	boolean isWhiteSpace = false;
    	    	int wordCount=0;
    	    	int charCount=0;
    		int currentChar;
    		while ((currentChar = input.read()) != -1) {
    				[COLOR="YellowGreen"][I]//Counting chars[/I][/COLOR]
    			charCount++;
    
    				[COLOR="YellowGreen"][I]//Counting words[/I][/COLOR]
    			isWhiteSpace = Character.isWhitespace((char)currentChar);
    			if(isWhiteSpace && !wasWhiteSpace) wordCount++;
    			wasWhiteSpace = isWhiteSpace;
    		}

  7. #7
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    I get an error saying that the input.read() must be declared or caught .....

    Java Code:
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    // Program to read characters on standard input, counting
    // how many words that contains and reports number on its
    // standard output.
    
    public class WordCount
    {
      public static void main(String [] args)
      {
        // there are 65536 different character values (2 bytes)
        // This array will already be set to zero in each element.
        int[] characterCount = new int[65536];
        
        
        // we will read the input as characters.
        InputStreamReader input = new InputStreamReader(System.in);
        
        // the number of words found so far.
        int allCharactersCount = 0;
        
        try
        {
          int currentCharacter;
          while ((currentCharacter = input.read()) != -1)
          {
            allCharactersCount++;
    	characterCount[currentCharacter]++;
          }
        }
        
       
        
        catch (IOException exception)
        {
          System.err.println(exception);
        }
        
        finally
        {
          try { input.close(); }
          catch (IOException exception)
            { System.err.println("could not close input " + exception); }
        }
        
        boolean wasWhiteSpace = false;
        boolean isWhiteSpace = false;
        int wordCount=0;
        int charCount=0;
        int currentChar;
        while ((currentChar = input.read()) != -1) 
        {
          //Counting chars
          charCount++;
    
         //Counting words
         isWhiteSpace = Character.isWhitespace((char)currentChar);
         if(isWhiteSpace && !wasWhiteSpace) 
         wordCount++;
         wasWhiteSpace = isWhiteSpace;
        }
        System.out.println("the number of words read was " + wordCount);
        
      }
    }
    This is the error i get.

    javac WordCount.java
    WordCount.java:52: unreported exception java.io.IOException; must be caught oreclared to be thrown
    while ((currentChar = input.read()) != -1)

    kind regards

    Shyam

  8. #8
    subith86 is offline Senior Member
    Join Date
    Jan 2011
    Location
    Bangalore, India
    Posts
    102
    Rep Power
    0

    Default

    Oh dear Shyam, what did you do... !!! Use some common sense.
    REPLACE your while loop with the one I provided. Not adding it at a later stage after try-catch.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,001
    Rep Power
    10

    Default

    Further evidence that spoonfeeding is not helping...
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    thank you all so much. I appreciate your help.

    :)

    Kind regards

    Shyam

Similar Threads

  1. Counting words
    By Wasley in forum New To Java
    Replies: 9
    Last Post: 01-30-2011, 11:57 PM
  2. problems with counting
    By worsethenu in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 03:33 PM
  3. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 03:43 AM
  4. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 09:42 PM
  5. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •