简体   繁体   English

为什么有时当我通过java中的isDigit方法检查字符是否为数字时,出现错误?

[英]Why sometimes when I check whether a character is a digit by method isDigit in java, I get error?

If I have 如果我有

Character.isDigit('53')

I get error 我得到错误

but if I have: 但是如果我有:

Character.isDigit('5')

I get a boolean value of TRUE. 我得到布尔值TRUE。

Why is that so? 为什么会这样?

'5' is a character. '5'是一个字符。 '53' is not a character. '53'不是字符。

Or, to look at it in terms of strings, "5" is a string with one character in it ( '5' ), and "53" is a string with two characters in it ( '5' , '3' ). 或者,从字符串的角度来看, "5"是其中包含一个字符( '5' )的字符串,而"53"是其中具有两个字符( '5''3' )的字符串。

A character is a single symbol. 字符是单个符号。 This means that '5' is a character, but since '53' has two symbols, it is not a character. 这意味着“ 5”是一个字符,但是由于“ 53”具有两个符号,因此它不是一个字符。

“ 5”和“ 53”都表示为字符串,其中第一个包含一个字符,第二个包含两个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符既不是Java中的字母也不是数字? - How do I check if a character is neither a letter nor a digit in Java? Character.isDigit() 错误:找不到适合 isDigit(String) 的方法 - Character.isDigit() error: no suitable method found for isDigit(String) 为什么我收到“资源泄漏:”<unassigned Closeable value> 即使我在 Java 中使用 close() 方法也会出错? - Why I get 'Resource leak: '<unassigned Closeable value>' ' error even when I use close() method in Java? java 方法 Character.isDigit(char) 返回 true,输入为 'O' - The java method Character.isDigit(char) returns true with an input of 'O' java中HashMap的containsKey方法是否会检查一个整数,如果我在里面放一个字符? - Will HashMap's containsKey method in java, check for an integer if I put a character in it? 为什么在隐藏片段时有时会得到堆栈跟踪? - Why do I sometimes get a stack trace when hiding a fragment? 如何在 java 中提取数字并检查它是否是质数? - How to extract digit and check whether it is prime digit or not in java? 调用start方法时为什么会出现编译错误? - Why do I get a compile error when calling start method? 为什么我在Java中收到此错误? - why I get this error in java? 为什么在 Java 中使用 Random 会出错? - Why do I get an error when using Random in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM