You are viewing a single comment's thread.

view the rest of the comments →

0
0

[–] thephantompoop ago  (edited ago)

You can do this easily by using String.split(" ") and getting the resulting array's length, unless your homework doesn't consider "??" a word. Then you'll have to use regular expressions which I can explain

0
0

[–] tragicwhale [S] ago 

Hey Phantom, It actually doesn't consider ?? a word. If the string was "This is ????? my ----- homework." It should calculate it to 4 words. Thanks for any help.

0
1

[–] thephantompoop 0 points 1 point (+1|-0) ago  (edited ago)

I had a whole explanation written out, but the page refreshed and i lost it and i need to write an essay soon -,-

So here's the run-down

after you assign the sentence to a string, you should use

sentence = sentence.replaceAll("[^\w\s]","");

This removes all non-whitespace and non-letter/number characters. You string will be "This is my homework".

After this,

word = sentence.split(" ").length;

This turns sentence into a static array of length 4:

[This, is, my, homework]

A handy site to explain what I just did:

Regex

A handy site for testing regex with a quick reference chart at the bottom:

Rubular