在 Java 中要将 String 转换为 int,我们可以使用两个内置方法,即 parseInt() 和 valueOf()。这些静态方法属于 java.lang 包的 Integer 类,如果字符串不是整数的有效表示,则会抛出 NumberFormatException。在Java中,String是'java.lang'包中的一个类,它存储一系列用双引号括起来的字符。并且,整数是存储数值的原始数据类型。在本文中,我们将讨论一些 Java 程序来说明如何将给定的字符串转换为整数。
将字符串转换为 int 的 Java 程序
Integer.parseInt()
Integer.valueOf()
让我们借助示例程序一一讨论这些方法。
使用 Integer.parseInt()
Integer.parseInt() 方法将字符串作为参数并将其解析为原始整数值。
语法
Integer.parseInt(String val);
示例
以下 Java 程序演示了如何使用 parseInt() 方法将 String 转换为 int。
方法
public class Example1 { public static void main( String args[] ) { // initializing a string String inputString = "99999"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.parseInt(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }
输出
Type of inputString: String The given String after converting into Integer: 99999 Type of given String after converting into Integer: Integer
使用 Integer.valueOf()
Integer.valueOf() 方法可以采用字符串、字符或 int 作为参数,但返回一个 Integer 对象,该对象保存解析的整数的值。
语法
Integer.valueOf(String val);
示例 1
这是另一个 Java 程序,它将展示如何借助 Integer.valueOf() 方法将 String 转换为 int。
public class Example2 { public static void main( String args[] ) { // initializing a string String inputString = "30072023"; // to check the datatype of string System.out.print("Type of inputString: "); System.out.println(inputString.getClass().getSimpleName()); // converting the string into an integer int newVal = Integer.valueOf(inputString); // printing the result System.out.println("The given String after converting into Integer: " + newVal); // to check the datatype of integer System.out.print("Type of given String after converting into Integer: "); System.out.println(((Object)newVal).getClass().getSimpleName()); } }
输出
Type of inputString: String The given String after converting into Integer: 30072023 Type of given String after converting into Integer: Integer
示例 2
前面我们提到过,如果传递的字符串不是整数的有效表示,则 parseInt() 和 valueOf() 方法会抛出 NumberFormatException。在此 Java 程序中,我们将传递一个包含字母字符而不是数字字符的字符串来显示异常。
public class Example3 { public static void main( String args[] ) { // initializing a string String inputString = "TutorialsPoint"; // converting the string to the integer int newVal = Integer.valueOf(inputString); // will give exception // printing the result System.out.println("The given String after converting into Integer: " + newVal); } }
输出
Exception in thread "main" java.lang.NumberFormatException: For input string: "TutorialsPoint" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:665) at java.base/java.lang.Integer.valueOf(Integer.java:992) at Example3.main(Example3.java:6)
结论
Integer.parseInt() 和 Integer.valueOf() 都是将字符串转换为整数的非常有用的方法。但是,Integer.valueOf() 方法的性能明显快于 Integer.parseInt() 方法。我们讨论了三个 Java 程序来说明这些方法的实际实现。
以上就是将一个字符串转换为整数的Java程序的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。