开发者

JAVA - logical operations on chars

开发者 https://www.devze.com 2023-02-20 10:46 出处:网络
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.

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 == '.') )
0

精彩评论

暂无评论...
验证码 换一张
取 消