Word Count

hey, i need help writing a piece of code that takes a text area and counts the number of words the user has written.thanks in advance
[147 byte] By [eviljoea] at [2007-10-1 0:42:14]
# 1
get textapply new StringTokenizer(text, " ");count tokens.
CeciNEstPasUnProgrammeura at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 2

How do you define a "word"? How many words are in the following sentence:

"Isn't it a pity ?"

"Isn't" could be one or two. Are you going to count the question mark? Will it have to handle other languages?

Depending on why you want to do this, the closest approximation might be to simply count the spaces and add one.

Dave.

dcmintera at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 3

> How do you define a "word"? How many words are in the

> following sentence:

>

> "Isn't it a pity ?"

>

> "Isn't" could be one or two. Are you going to count

> the question mark? Will it have to handle other

> languages?

>

> Depending on why you want to do this, the closest

> approximation might be to simply count the spaces and

> add one.

>

> Dave.

i don't need it to be fool proof so the spaces plus one would be ok.

would you have the code for that pray tell?

eviljoea at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 4

> i don't need it to be fool proof so the spaces plus

> one would be ok.

> would you have the code for that pray tell?

String data = "some text";

int numberOfWords = data.split(" ").length;

/Kaj

kajbja at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 5

> > i don't need it to be fool proof so the spaces

> plus

> > one would be ok.

> > would you have the code for that pray tell?

>

>

> > String data = "some text";

> int numberOfWords = data.split(" ").length;

>

>

> /Kaj

thank you very much. how do i make the String data = JtextArea?

I promise to stop annoying you with my simpleton questions now.

eviljoea at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 6
> thank you very much. how do i make the String data => JtextArea?> I promise to stop annoying you with my simpleton> questions now.[code]String data = textArea.getText();/code]/Kaj
kajbja at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 7

> > How do you define a "word"? How many words are in

> the

> > following sentence:

> >

> > "Isn't it a pity ?"

> >

> > "Isn't" could be one or two. Are you going to

> count

> > the question mark? Will it have to handle other

> > languages?

> >

> > Depending on why you want to do this, the closest

> > approximation might be to simply count the spaces

> and

> > add one.

> >

> > Dave.

>

> i don't need it to be fool proof so the spaces plus

> one would be ok.

> would you have the code for that pray tell?

there are four words in that sentence, its quite basic. "isn't" is one word, how could it possibly be two!? "is not" is two words. a question mark is not a word so thats a stupid question. it won't handle other languages because English is the only one that matters. Rule Brittania

eviljoea at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 8

> > thank you very much. how do i make the String data

> =

> > JtextArea?

> > I promise to stop annoying you with my simpleton

> > questions now.

>

> [code]

> String data = textArea.getText();

> /code]

>

> /Kaj

thank you very much for your patience and great assistance

eviljoea at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 9
use wc**no, this is not a toilet. it is a unix command.
filestreama at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 10
> *no, this is not a toilet. it is a unix command.Unlike flush().
CeciNEstPasUnProgrammeura at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 11
> > *no, this is not a toilet. it is a unix command.> > Unlike flush().das Klo
filestreama at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 12
Is there a command to count the amount of characters?
BrownManUPSa at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 13
> Is there a command to count the amount of characters?No. But you can write one. Start by defining in your head (or on paper) what you consider to be a character.
yawmarka at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 14
I actually want to count the number of digits....like if user gave1320402it has 7 digits, or 7 chars.
BrownManUPSa at 2007-7-8 0:56:34 > top of Java-index,Security,Event Handling...
# 15

> I actually want to count the number of digits....like

> if user gave

>

> 1320402

>

> it has 7 digits, or 7 chars.

Your definition is incomplete. Determine your expected result if the user provides the following input (for example)t:

1320402

132 0402

1 32 0401

0010 0011

0x00

0x01

0xFF

$12345

123.45

123_45

12!

4*5

a1b2 c3d4

two

twenty-seven

one hundred

-25

555-55-5555

(555) 555-1212

123456

yawmarka at 2007-7-20 1:51:51 > top of Java-index,Security,Event Handling...
# 16

String s = "Hello";

char word [] ;

word = s.toCharArray() // word references an array of five characters

To count the number of character in the Array : int nb = word.length

nextOnea at 2007-7-20 1:51:51 > top of Java-index,Security,Event Handling...
# 17
> Is there a command to count the amount of characters?wc -c
tsitha at 2007-7-20 1:51:51 > top of Java-index,Security,Event Handling...
# 18
that worked so much thanks
BrownManUPSa at 2007-7-20 1:51:51 > top of Java-index,Security,Event Handling...