This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Hi all. I want to create a small application that takes a file as a parameter and performs the following tasks : counts the number of characters. counts the number of white spaces. counts the number of lines. counts the number of words. search for a specific word. but the problem is that I am't sure about these algorithms. so would you mind giving me some tips to create these methods ? (like how could I know that this line has ended and how to know that the word has ended) here is some code :
any corrections about the previous code ? I think there is some thing wrong with counting the spaces and chars, what do you think ? thanks alot.
Thanks. I want to count the english words. but I am confused, in order to count spaces, should I use : isWhitespace( ) or is SpaceChar( ) ?? which method in Character class counts the words ?? thanks again..
I'm dyin to write some hints on word counting but making myself wait until you try it first. You get all the fun!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Aug 07, 2003
Originally posted by John Todd: in order to count spaces, should I use : isWhitespace( ) or is SpaceChar( ) ??
What does the program specification say you should count: spaces or whitespace? Read the JavaDocs for both methods and see which matches the spec and use it.
which method in Character class counts the words ??
How could a Character be able to tell you anything about words (other than single-letter words like "I" and "a")? Think about how you detect English words (try defining one for starters) in the context of a text file. Then translate that into a sequence of logic steps and finally code.
What that tells me is that you should never read JavaDocs before coffee. No wait, what that tells me is that the three I mentioned above are included in that much wider definition. But I would bet you that when your program is tested, only the three I mentioned will be considered (maybe carriage return if tested on a Mac). Regardless, using Character.isWhitespace(char) will count them all correctly.
The real trick is how to define and detect an "English word." How many words do the following sentences have?
This sentence has 5 or 7 words
Is punctuation part of the words preceding it or a separate word?
Perhaps you will only get letters and whitespace so it is easy
Yahoooooooooooooooooooooo I found it, I found how to count the words. but I have a question : look at this code please :
I am increasing the number of characters every time I encounter a unicode space or a Java space, which ofcourse will cause the sum to produce a wrong result. if I want to count the number of charcs in a file, which method should I use : isWhitespace or isSpaceChar ? note : what is the difference between the char and the letter ? thanks ranchers.
Joined: Aug 07, 2003
Think of it like this. For every character in the file, you want to take a set of actions.
Increment the total character counter.
If it's a whitespace character, increment the whitespace counter.
If it's a letter character, increment the letter counter.
Note where the various counter increment steps take place. This will solve your problem with over-counting total characters. In fact, Stan pointed this out in an earlier reply. [ November 08, 2004: Message edited by: David Harkness ]