简体   繁体   English

java - 当字符串以零结尾时,如何解决NumberFormatException?

[英]How can I solve NumberFormatException when string endswith zero in Java?

I have written code to convert string into int but I'm getting error when string ends with zero '0'.我已经编写了将字符串转换为 int 的代码,但是当字符串以零“0”结尾时出现错误。

I have tried this,我试过这个,

int val=Integer.parseInt(s);

Input string,输入字符串,

9876543210

You might be trying to parse a string which does not contain a parsable integer.您可能正在尝试解析不包含可解析整数的字符串。 as document Integer.parseInt(String str) says.正如文档Integer.parseInt(String str)所说。

Parses the string argument as a signed decimal integer.将字符串参数解析为有符号十进制整数。 The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\-') to indicate a negative value or an ASCII plus sign '+' ('\+') to indicate a positive value.字符串中的字符必须都是十进制数字,除了第一个字符可以是 ASCII 减号 '-' ('\-') 表示负值或 ASCII 加号 '+' ('\+')表示正值。 The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.返回结果整数值,就像参数和基数 10 作为参数提供给 parseInt(java.lang.String, int) 方法一样。

Throws: NumberFormatException - if the string does not contain a parsable integer.抛出: NumberFormatException - 如果字符串不包含可解析的整数。

With some example we can understand it.通过一些例子我们可以理解它。


String s = "3t0";
int val=Integer.parseInt(s);
// Will throw exception 

String s = "9876543210"; 
int val=Integer.parseInt(s);
// Will throw an Exception because  9876543210 is not an Integer see the Integer range of values (-2^31 to 2^31-1) 


String s = "433"
int val=Integer.parseInt(s);
// Will successfully parse 


暂无
暂无

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

相关问题 如何解决 java.lang.NumberFormatException:对于输入字符串:“TooLow” - How can I solve java.lang.NumberFormatException: For input string: “TooLow” 如何解决Java中的NumberFormatException? - How to solve NumberFormatException in Java? 当尝试从String转换非常大的数字时,如何在Java中解决NumberFormatException - How to solve NumberFormatException in Java when trying to convert a very large number from String 如何解决 java.lang.NumberFormatException: empty String - How to solve java.lang.NumberFormatException: empty String 如何解决 java.lang.NumberFormatException:对于输入字符串:“396.00” - How to solve java.lang.NumberFormatException: For input string: "396.00" 将字符串解析为double / integer时如何解决NumberFormatException - How to solve NumberFormatException when parsing string to double/integer 如何解决XStream中的零长度字符串异常 - how can I solve zero length string exception in XStream 如何在Java中避免NumberFormatException? - How can I avoid a NumberFormatException in Java? 我该如何解决:java.lang.RuntimeException:无法启动活动ComponentInfo java.lang.NumberFormatException:对于输入字符串:“ s%”? - How do I solve: java.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NumberFormatException: For input string: “s%”? 我不知道如何解决代码,因为我不断收到 java.lang.NumberFormatException 的反馈:对于输入字符串:“” - I don't know how to solve the code as i keep having the feedback of java.lang.NumberFormatException: For input string: ""
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM