I created a method to basically detect white space characters. I go through a string and check each character for white space. If it is a white space character, I return true, and if it’s not, I return false. However, I get a compilation error, stating “missing return statement”. As I already have two return statements “true” and “false”, I can’t see why there is an error. Can you help me out or point me in the right direction? Thanks in advance.
public boolean isWhitespace()
{
for (int i=0; i<string.length(); i++)
{
if (Character.isWhitespace(i))
{
return true;
}
else
{
return false;
}
}
}
You are looping over the length of the String, yet trying to return inside that loop. The logic doesn’t make sense.
Think about the problem you are trying to solve – do you want to test if a character is a whitespace, or if an entire String contains at least one whitespace character? For the latter:
EDIT: A much simpler approach, if you’re into that sorta thing 😉 –