I am trying to split a string so i can count the words but it is not splitting carriage returns...only spaces.
I have a text file with about 10 lines of text on it with line spaces.
When the string is printed the last word of the line and the beginning word of the next line are joined together.
How can I get it so the words are seperated?
public void wordCount()
String result = textWord.split("\\s");
System.out.println("Number of words: "+result.length);
The file has 61 words but the result.length is reporting 53 words
01-08-2007, 08:08 PM
Have you tried to do this with the Scanner class rather than the String class's split method? The default delimiter for Scanner is whitespace (which, it sounds like, you are looking for). It will also take the filename as argument for its constructor, so will do away with your BufferedReader procedure you wrote about, before.