首頁 > Java > java教程 > 主體

將一個字串轉換為整數的Java程序

WBOY
發布: 2023-08-30 20:41:18
轉載
1161 人瀏覽過

將一個字串轉換為整數的Java程序

在 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。

方法

  • 初始化一個將被轉換為整數的字串。

  • 接下來,使用 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中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!