Java 程序将字符串转换为整数
在 Java 中,将字符串转换为整数,我们可以使用两个内置方法,即 parseInt() 和 valueOf()。这些静态方法属于 java.lang 包的 Integer 类,如果字符串不是整数的有效表示形式,则会抛出 NumberFormatException。在 Java 中,String 是 'java.lang' 包的一个类,用于存储用双引号括起来的一系列字符。而整数是一种基本数据类型,用于存储数值。在本文中,我们将讨论一些 Java 程序,这些程序说明了如何将给定的字符串转换为整数。
Java 程序将字符串转换为整数
如前所述,我们将使用以下方法将字符串转换为整数:
Integer.parseInt()
Integer.valueOf()
让我们借助示例程序逐一讨论这些方法。
使用 Integer.parseInt()
Integer.parseInt() 方法将字符串作为参数,并将其解析为基本整数值。
语法
Integer.parseInt(String val);
示例
以下 Java 程序演示了如何使用 parseInt() 方法将字符串转换为整数。
方法
初始化一个将转换为整数的字符串。
接下来,使用 getClass() 和 getSimpleName() 方法检查并打印已定义字符串的类型。
现在,使用 parseInt() 方法将字符串转换为整数,并将结果存储在整数变量中。
最后,检查并打印数据类型以确认字符串是否已转换为整数。
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() 方法可以将字符串、字符或整数作为参数,但返回一个包含已解析整数值的 Integer 对象。
语法
Integer.valueOf(String val);
示例 1
这是另一个 Java 程序,它将展示如何使用 Integer.valueOf() 方法将字符串转换为整数。
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
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP