在 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中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6
視覺化網頁開發工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),