Welcome to Dream.In.Code
Become a Java Expert!

Join 149,456 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,136 people online right now. Registration is fast and FREE... Join Now!




simple word counter problem

2 Pages V  1 2 >  
Reply to this topicStart new topic

simple word counter problem, counting words

deathangel188
10 Oct, 2007 - 05:02 AM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
CODE
import java.io.*;

public class  wordcount{
  public static void main (String[] args)  {
        long numlines = 0;
        long numwords = 0;
        int counts=0;
        int space;
        int now;
        String paragraph;
            paragraph=Keyin.inString("please input a paragraph");
            do{
            space = paragraph.indexOf(' ');
            counts++;
              }while(counts==-1);
        do{
            now=paragraph.length();
            numwords++;            
             }while(numwords<paragraph.length());
                    System.out.println("word="+numwords);        
                        }
                                            }


this is the code i have so far, but it's counting characters not words any idea i could count words?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Simple Word Counter Problem
10 Oct, 2007 - 05:10 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
If you can define what a word is, it will make the solution quite straightforward.
User is offlineProfile CardPM
+Quote Post

deathangel188
RE: Simple Word Counter Problem
10 Oct, 2007 - 05:12 AM
Post #3

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
the user suppose to enter a paragraph and the word counter suppose to count how many word it is in this paragraph.(space were not count)
User is offlineProfile CardPM
+Quote Post

deathangel188
RE: Simple Word Counter Problem
10 Oct, 2007 - 06:31 AM
Post #4

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
this is the code i'm currently have

QUOTE(deathangel188 @ 10 Oct, 2007 - 07:31 AM) *

CODE
public class  wordcount{
  public static void main (String[] args)  {
        long numlines = 0;
        long numwords = 0;
        int counts=0;
        int space=0;
        int now;
        char spaces;
        String paragraph;
        paragraph=Keyin.inString("please input a paragraph");
        do{
            space = paragraph.indexOf(' ');
            numwords++;
            space = paragraph.indexOf(' ',space);
        }while(space==-1);
            System.out.println("words="+ numwords);
                    }
                                        }


this is the code i'm currently have


This post has been edited by deathangel188: 10 Oct, 2007 - 11:16 AM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Simple Word Counter Problem
10 Oct, 2007 - 07:22 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I meant what do you treat as a word: consecutive letters separated by anything that is not a letter, or digits count, too, etc. You should not only look for spaces, but for things like :,.;:, etc. and tabs, so on. Also, if there are several spaces after eachother, they don't surround a word.
There are generally two ways to accomplish this:
a ) use the Character.isLetter and Character.isDigit functions with a primitive statemachine (a boolean variable named isInWord maybe)
b ) or create a regexp with a well crafted Pattern and Matcher (this might very well be an overkill for your assignment, but proove very useful later)
User is offlineProfile CardPM
+Quote Post

deathangel188
RE: Simple Word Counter Problem
10 Oct, 2007 - 07:35 AM
Post #6

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
QUOTE(1lacca @ 10 Oct, 2007 - 08:22 AM) *

I meant what do you treat as a word: consecutive letters separated by anything that is not a letter, or digits count, too, etc. You should not only look for spaces, but for things like :,.;:, etc. and tabs, so on. Also, if there are several spaces after eachother, they don't surround a word.
There are generally two ways to accomplish this:
a ) use the Character.isLetter and Character.isDigit functions with a primitive statemachine (a boolean variable named isInWord maybe)
b ) or create a regexp with a well crafted Pattern and Matcher (this might very well be an overkill for your assignment, but proove very useful later)


you think i don't want do that? i treat only letter as word, my teacher said I can't use anything beside the thing i learn, it's stupid.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Simple Word Counter Problem
10 Oct, 2007 - 07:51 AM
Post #7

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Lol, I see. Then what can you use? Is the Character class allowed? Or String.charAt? or only String.indexOf?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Simple Word Counter Problem
10 Oct, 2007 - 08:14 AM
Post #8

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE
do{
space = paragraph.indexOf(' ');
numwords++;
space = paragraph.indexOf(' ',space);
}while(space==-1);

Anyway, the main problem here is two-fold:
- you start from the first space in every iteration. (second line)
- the while condition is actually checking if it could not find a space.
User is offlineProfile CardPM
+Quote Post

sl4ck3r
RE: Simple Word Counter Problem
10 Oct, 2007 - 09:26 AM
Post #9

D.I.C Head
Group Icon

Joined: 22 Sep, 2007
Posts: 119


Dream Kudos: 25
My Contributions
have you learned the StringTokenizer class? if so this is really easy.
http://java.sun.com/javase/6/docs/api/java...gTokenizer.html
just add symbols you want to split on to it then use the countTokens() to get the number of total tokens.
User is offlineProfile CardPM
+Quote Post

deathangel188
RE: Simple Word Counter Problem
10 Oct, 2007 - 11:15 AM
Post #10

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
what i mean by the while condition is it stop when it can't find any more space. or am i just wrong can you explain to me how would i fix it?
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Simple Word Counter Problem
10 Oct, 2007 - 12:02 PM
Post #11

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE(sl4ck3r @ 10 Oct, 2007 - 07:26 PM) *

have you learned the StringTokenizer class? if so this is really easy.
http://java.sun.com/javase/6/docs/api/java...gTokenizer.html
just add symbols you want to split on to it then use the countTokens() to get the number of total tokens.

Looking at the example, they probably did not so far. Anyway, the String.split method would do the same thing.


QUOTE
what i mean by the while condition is it stop when it can't find any more space. or am i just wrong can you explain to me how would i fix it?


the condition is fine, but you should move the part that resets the value of your variable named 'space' out of the loop, so the search doesn't start from the beginning all the time - maybe another loop construct will be needed as well, but do ... while could work, too.
User is offlineProfile CardPM
+Quote Post

deathangel188
RE: Simple Word Counter Problem
10 Oct, 2007 - 03:57 PM
Post #12

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 25


My Contributions
can someone fix my code for me please i'm really confused
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 01:23PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month