Java - 将字符串转换为 int
在 Java 中,要将 String 转换为 int,您可以使用 Integer.parseInt() 或 Integer.valueOf().Integer.parseInt()
String nombre_str = 22”;Runtime:
int number = Integer.parseInt(nombre_str);
System.out.println(数字);
22
Integer.valueOf()
Integer.valueOf() 将返回一个对象 整数.String nombre_s=22”;Execution
int number = Integer.valueOf(nombre_s);
System.out.println(数字);
22
如果字符串因包含字母字符而不被接受,例如,NumberFormatException 将被触发.
String nombre_s=9Ab”;Output:
int number = Integer.parseInt(nombre_s);
System.out.println(数字);
线程main”中的异常 java.lang.NumberFormatException: 对于输入字符串: 9Ab”
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
资源:
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf(java.lang.String, int)
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)答>