Results 1 to 10 of 10
Thread: Counting Words
- 03-02-2011, 01:36 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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.
Any suggestions or assistance would be most appreciated :)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); } }
Kind regards
Shyam
- 03-02-2011, 01:56 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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
Doesn't make any sense.Java Code:int[] wordCount = new int[65535];
- 03-02-2011, 02:06 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
ShyamLast edited by Shyamz1; 03-02-2011 at 02:11 PM.
- 03-02-2011, 02:06 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 67
- Rep Power
- 0
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.
- 03-02-2011, 02:16 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
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
- 03-02-2011, 02:37 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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; }
- 03-02-2011, 04:31 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
I get an error saying that the input.read() must be declared or caught .....
This is the error i get.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); } }
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
- 03-02-2011, 05:10 PM #8
Senior Member
- Join Date
- Jan 2011
- Location
- Bangalore, India
- Posts
- 102
- Rep Power
- 0
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.
- 03-02-2011, 05:17 PM #9
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!
- 03-03-2011, 01:12 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
Counting words
By Wasley in forum New To JavaReplies: 9Last Post: 01-30-2011, 10:57 PM -
problems with counting
By worsethenu in forum New To JavaReplies: 4Last Post: 12-06-2010, 02:33 PM -
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11-06-2009, 02:43 AM -
Recursive Counting
By zlwilly in forum New To JavaReplies: 1Last Post: 01-29-2009, 08:42 PM -
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10-22-2008, 01:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks