JAVA - logical operations on chars
I am creating a file reading program. I need to filter out any char that is not '0-9' or '.'.
Any char other then these needs to trigger an IF statement.
Here is what I tried -
if ( ( ((char)c < '0') || ((char)c > '9') ) || ((char)c != '.') )
or-
( ( ((char)c != '0' ) || ((char)c != '.' ) || ((char)c != '1' ) || 开发者_如何学C((char)c != '2' ) || ((char)c != '3' ) || ((char)c != '4' ) || ((char)c != '5' ) || ((char)c != '6' ) || ((char)c != '7' ) || ((char)c != '8' ) || ((char)c != '9' ) ))
neither of which worked.
if(Character.isDigit(c) || c == '.')
{
}
Any char that is not '.' will cause this if statement to be true, to fix it (and I take the first as an example, but it applies also to the second):
if ( ( ((char)c < '0') || ((char)c > '9') ) && ((char)c != '.') )
alternatively, you can write
if (!( ((char)c >='0' && (char) c <='9') || (char) c == '.') )
精彩评论