[–] 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
[–] 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.
[–] thephantompoop 0 points 1 point 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:
A handy site for testing regex with a quick reference chart at the bottom:
[–] TheGreatNico ago
Count the spaces, not the characters, make exceptions for non-words
[–] tragicwhale [S] ago
I can't just count the spaces though right because if I have "This is ?? a --- sentence." It'll count 7 spaces, unless I'm not reading your aid correctly. Thanks for your help.