I am working on a program that will count words that are typed or inserted into the text area. He correctly counts words if he does not have double space. I use the split method for this and use a for loop to count words for objects.
here is the simplest form of part of the code that caused some problems ...
public static void main(String[] args) { String string = "Java C++ C#"; String[] str; int c=0; str = string.split(" "); for(String s:str){ if(s.equals(" ")) System.out.println("skipped space"); else{ System.out.println("--"+s); c++; } } System.out.println("words; " + c); }
im trying to check if the string contained in the object s contains a space, but how I do it does not work.
I want it to be output as follows
--Java skipped space --C++ --C
the words; 3
but the result
--Java -- --C++ --C
Any suggestions on how I can solve this? or in what part did I have a problem? thank you in advance.
source share