
在 Java 中要将 String 转换为 int,我们可以使用两个内置方法,即 parseInt() 和 valueOf()。这些静态方法属于 java.lang 包的 Integer 类,如果字符串不是整数的有效表示,则会抛出 NumberFormatException。在Java中,String是'java.lang'包中的一个类,它存储一系列用双引号括起来的字符。并且,整数是存储数值的原始数据类型。在本文中,我们将讨论一些 Java 程序来说明如何将给定的字符串转换为整数。
如前所述,我们将使用以下方法将字符串转换为整数 -
Integer.parseInt()
Integer.valueOf()
立即学习“Java免费学习笔记(深入)”;
让我们借助示例程序一一讨论这些方法。
Integer.parseInt() 方法将字符串作为参数并将其解析为原始整数值。
Integer.parseInt(String val);
以下 Java 程序演示了如何使用 parseInt() 方法将 String 转换为 int。
初始化一个将被转换为整数的字符串。
Perl学习手札是台湾perl高手写的一篇文章,特打包为chm版,方便大家阅读。 关于本书 1. 关于Perl 1.1 Perl的历史 1.2 Perl的概念 1.3 特色 1.4 使用Perl的环境 1.5 开始使用 Perl 1.6 你的第一个Perl程序 2. 标量变量(Scalar) 2.1 关于标量 2.1.1 数值 2.1.2 字符串 2.1.3 数字与字符串转换 2.2 使用你自己的变量 2.3 赋值 2.3.1 直接设定 2.3.2 还可以这样 2.4 运算 2.5 变量的输出/输入 2.
0
接下来,使用 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() 方法可以采用字符串、字符或 int 作为参数,但返回一个 Integer 对象,该对象保存解析的整数的值。
Integer.valueOf(String val);
这是另一个 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
前面我们提到过,如果传递的字符串不是整数的有效表示,则 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程序的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号