Java – Convert String to int
来源:互联网 发布:淘宝上怎么延迟收货 编辑:程序博客网 时间:2024/05/22 03:53
In Java, you can use Integer.parseInt()
to convert a String
to int
.
1. Integer.parseInt()
Examples
Example to convert a String
“10” to an primitive int
.
String number = "10"; int result = Integer.parseInt(number); System.out.println(result);
Output
10
2. Integer.valueOf()
Examples
Alternatively, you can use Integer.valueOf()
, it will returns an Integer
object.
String number = "10"; Integer result = Integer.valueOf(number); System.out.println(result);
Output
10
Note
In summary, parseInt(String)
returns a primitive int, whereas valueOf(String)
returns a new Integer()
object.
3. NumberFormatException
If the string does not contain a parsable integer, a NumberFormatException
will be thrown.
String number = "10A"; int result = Integer.parseInt(number); System.out.println(result);
Output
Exception in thread "main" java.lang.NumberFormatException: For input string: "10A" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source)
0 0
- Java – Convert String to int
- java int string convert
- Java Convert String & Int
- C++ convert int to string
- Convert string type to int
- java int and string convert
- long[] or int[] convert to string[] method
- convert ip address string to unsigned int
- convert unsigned int to ip string
- Convert int/long to hex string.
- Convert string to int in C++
- Java – Convert String To Enum Object
- Failed to convert property value of type [java.lang.String] to required type [int]
- Failed to convert property value of type 'java.lang.String' to required type 'int' for property
- Java: How to convert int[] to byte[]
- java String to int
- Convert int? to int
- 字符串转换为数字:String:convert a string to int
- ssh无密码登陆
- 当出现的Interexploer 终止站点的操作的提示框
- Win7上java开发环境搭建
- 在《Android PorterDuff.Mode图形混合处理 》这篇博客中,我们讲解了PorterDuff.Mode对图形混合的处理。这篇我们将通过图形混合的原理,来制作一个手动擦除蒙版显示底层图片
- hello spring
- Java – Convert String to int
- ListView 显示不同布局
- 智能指针 weak_ptr
- c#集合类汇总(上)
- 点击cell,在cell下方创建一个新的cell,再次点击,删除该cell
- 04概要设计说明书
- Switch能否用string做参数?
- leetcode之路050 Pow(x, n)
- ycmsmocbpbgq