A.Nur A.Nur - 12 days ago 5
Java Question

Method using Character.isDigit() Returns false for everything

public boolean isANumber (String s)
{
for (int i = 0; i < s.length(); i++)
{
if (!Character.isDigit(s.charAt(i)));
{
return false;
}
}
return true;
}


I got this method from Apache Commons Lang and edited it a bit to be shorter. Whether the input is a letter or a digit, the output is always false. Could anyone tell me why please? :)

Thanks in advance,

A.Nur

Answer

The corrected code

public boolean isANumber (String s)  
{
   for (int i = 0; i < s.length(); i++)
   {
      if (!Character.isDigit(s.charAt(i)))
      {
        return false;
      }
   }
   return true;
}

That is a good argument for putting the brace at the end of the line, instead of at the beginning of the following line.