I'm quite new to coding and have run across a problem I cant seem to fix.
I want to write some code that counts words in a sentence, simple enough:
public static void countNoOfWords(String string) throws Exception
{
int countWord = 0;
boolean word = false;
int endOfLine = string.length() - 1;
string.replaceAll("\\p{Punct}+", "a");
for (int i = 0; i < string.length(); i++)
{
if (Character.isLetter(string.charAt(i)) && i != endOfLine){
word = true;
}
else if (!Character.isLetter(string.charAt(i)) && word){
countWord++;
word = false;
}
else if (Character.isLetter(string.charAt(i)) && i == endOfLine){
countWord++;
}
}
System.out.println("\nTotal number of words: " + countWord);
selectAnalysis(string);
}
But I want it to ignore words made entirely of special characters such as £,$,& etc. so if I insert a sentence such as "Hello ^^&% mi$$" the answer will still be 3, where as I want it to be 2.
Ive tried a number of different solutions including turning all the words into strings and placing them in an array, replacing special characters with letters, counting spaces.Ive looked around for posts on here and seen some similar questions but not the answer to mine, I'm really quite stuck!
Thank for your help
P.S.Sorry if this isn't posted correctly or if its missing something, or if the question was answered before I've read a few of these posts but this is my first one.