如果字符已经是java中的数字,则将字符转换为整数
我正在尝试读取字符串的字符来获取数值。 String 卡号 = in.next();
int currentIndex = cardNumber.length() - 1;
while (currentIndex >= 0)
{
int smallValue;
smallValue = Character.getNumericValue(currentIndex);
当smallValue运行时,它没有给我这个数字。只是一个-1
I'm trying to read a string's character to get the numeric value.
String cardNumber = in.next();
int currentIndex = cardNumber.length() - 1;
while (currentIndex >= 0)
{
int smallValue;
smallValue = Character.getNumericValue(currentIndex);
when smallValue runs, its not giving me the number. just a -1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用Character.digit(),但是您使用数组索引而不是该索引处的元素值来调用它。
You should be using Character.digit(), but you are calling it with the array index instead of the element value at that index.
您正在将
currentIndex
传递给Character.getNumericValue
。我认为您实际上想传递cardNumber
的字符之一。You are passing
currentIndex
toCharacter.getNumericValue
. I think you actually want to pass one of the characters ofcardNumber
.我想这就是你想要的:
I think this is what you want:
假设
cardNumber
是一个String
,我相信这就是您想要的:Assuming
cardNumber
is aString
, I believe this is what you want: