Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Count number of whitespaces?!

843789Oct 2 2009 — edited Oct 5 2009
Hello,

I would like to count the number of whitespaces(Tab,Return,Spaces) in a given file I have tried so many times but I don't know why it's not working here is my code.
while (fileScan.hasNext()) { 
			count++;
			String str=fileScan.next();
		
			char[] chars =str.toCharArray();
			int i;
			for (i=0; i<str.length(); i++)		
			{		
				//Uppercase Letters
				if (Character.isUpperCase(chars))				
caps++;
//Lowercase Letters
else if(Character.isLowerCase(chars[i]))
lows++;
//Other Characters
else if(!Character.isLetter(chars[i]))
OtherCharachters++;
//Whitespaces
else if((chars[i]==' ')||(chars[i]=='\n')||(chars[i]=='\t'))
numOfWhitespaceChars++;
}
}
Thanks all inadvance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2009
Added on Oct 2 2009
33 comments
635 views