Java 将字符串转换为整数
在Java中,要将字符串转换为整数,我们可以使用两种内置方法,即parseInt()和valueOf()。这些静态方法属于java.lang包的Integer类,如果字符串不是整数的有效表示形式,则会抛出NumberFormatException。在Java中,String是’java.lang’包的类,用于存储双引号内的一系列字符。而整数是一种存储数值的基本数据类型。在本文中,我们将讨论一些Java程序,以示例说明如何将给定字符串转换为整数。
将String转换为int的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()方法将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程序,演示了这些方法的实际应用。