首页 > Java > java教程 > 正文

将一个字符串转换为整数的Java程序

WBOY
发布: 2023-08-30 20:41:18
转载
1389人浏览过

将一个字符串转换为整数的java程序

在 Java 中要将 String 转换为 int,我们可以使用两个内置方法,即 parseInt() 和 valueOf()。这些静态方法属于 java.lang 包的 Integer 类,如果字符串不是整数的有效表示,则会抛出 NumberFormatException。在Java中,String是'java.lang'包中的一个类,它存储一系列用双引号括起来的字符。并且,整数是存储数值的原始数据类型。在本文中,我们将讨论一些 Java 程序来说明如何将给定的字符串转换为整数。

将字符串转换为 int 的 Java 程序

如前所述,我们将使用以下方法将字符串转换为整数 -

让我们借助示例程序一一讨论这些方法。

使用 Integer.parseInt()

Integer.parseInt() 方法将字符串作为参数并将其解析为原始整数值。

语法

Integer.parseInt(String val);
登录后复制

示例

以下 Java 程序演示了如何使用 parseInt() 方法将 String 转换为 int。

方法

  • 初始化一个将被转换为整数的字符串。

    Perl学习手札 chm版
    Perl学习手札 chm版

    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.

    Perl学习手札 chm版 0
    查看详情 Perl学习手札 chm版
  • 接下来,使用 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() 方法可以采用字符串、字符或 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程序的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号