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 程式,這些程式說明了這些方法的實際實現。

更新於: 2023年8月10日

360 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.