• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Help - Counting White spaces

lil layzie

Gawd
Joined
Jan 7, 2004
Messages
755
I'm trying to count how many white spaces there are in this particular string.
The string is "say some thing"
Which would mean there is 2 white spaces.

Code:
int count = 0;
String aString = "say some thing";
		
	for (int n=0; n<aString.length();n++)
	{
		if (aString.charAt(n)==' ');
		{
		count++;
		}
	}
I think there is a problem with my aString.charAt(n)== because it is adding 1 to count for every character, not just a whitespace.

How do i define a white space so i can make an if statement.
 
First delete the semi colon at the end of the if statement. Then try using Character.isWhitespace(aString.charAt(n)) instead.
 
... isnt their also something like string.length(); too?
Forgive me I'm a bit of a newbie.. and I cannot remember the syntax perfectly but I know we've used something like that in our classes :)
 
Back
Top