給定包含數字作為字符的字符串 str,任務是在 Java 中將此給定字符串轉換為整數。
例子:
Input: str = "1234" Output: 1234 Input: str = "213s" Output: 0 Since the String contains other than digits, hence the integer value will be 0
- 方法一:使用整數.parseInt()方法
這是將字符串轉換為整數的最簡單方法。此函數將字符串參數解析為有符號十進製整數。用法:
public static int parseInt(String s) throws NumberFormatException
下麵是上述方法的實現:
// Java program to convert String to int // using Integer.parseInt() method import java.io.*; class GFG { // Function to convert String to integer public static int convert(String str) { int val = 0; System.out.println("String = " + str); // Convert the String try { val = Integer.parseInt(str); } catch (NumberFormatException e) { // This is thrown when the String // contains characters other than digits System.out.println("Invalid String"); } return val; } // Driver code public static void main(String[] args) { String str = "1234"; int val = convert(str); System.out.println("Integer value = " + val); System.out.println(); str = "123s"; val = convert(str); System.out.println("Integer value = " + val); } }
輸出:String = 1234 Integer value = 1234 String = 123s Invalid String Integer value = 0
- 方法二:使用 Guava 庫的 Ints::tryParse 方法
另一種將 String 轉換為整數的方法是使用 Guava 庫的 Ints::tryParse 方法。它類似於 Integer.parseInt() 方法,但這種方法更加簡潔和強大。用法:
public static Integer tryParse(String s)
下麵是上述方法的實現:
// Java program to convert String to int // using Ints::tryParse method import java.io.*; import java.util.*; import com.google.common.primitives.Ints; class GFG { // Function to convert String to integer public static int convert(String str) { int val = 0; System.out.println("String = " + str); // Convert the String val = Optional.ofNullable(str) .map(Ints::tryParse) .orElse(0); return val; } // Driver code public static void main(String[] args) { String str = "1234"; int val = convert(str); System.out.println("Integer value = " + val); System.out.println(); str = "123s"; val = convert(str); System.out.println("Integer value = " + val); } }
輸出:String = 1234 Integer value = 1234 String = 123s Integer value = 0
相關用法
- Java TimeZone getOffset(int, int, int, int, int, int)用法及代碼示例
- Java ZoneOffset ofHoursMinutesSeconds(int, int, int)用法及代碼示例
- Java SimpleTimeZone setStartRule(int, int, int)用法及代碼示例
- Java SimpleTimeZone setEndRule(int, int, int)用法及代碼示例
- Java HijrahDate of(int, int, int)用法及代碼示例
- Java JapaneseDate of(int, int, int)用法及代碼示例
- Java JapaneseDate of(JapaneseEra,int, int, int)用法及代碼示例
- Java MinguoChronology date(int, int, int)用法及代碼示例
- Java MinguoDate of(int, int, int)用法及代碼示例
- Java ThaiBuddhistChronology date(int, int, int)用法及代碼示例
- Java JapaneseChronology date(int, int, int)用法及代碼示例
- Java IsoChronology date(int, int, int)用法及代碼示例
- Java PrintWriter write(String, int, int)用法及代碼示例
- Java Writer write(String, int, int)用法及代碼示例
- Java StringWriter write(String, int, int)用法及代碼示例
- Java Math subtractExact(int a , int b)用法及代碼示例
- Java Math addExact(int a, int b)用法及代碼示例
- Java IntMath.checkedSubtract(int a, int b)用法及代碼示例
- Java IntMath.checkedMultiply(int a, int b)用法及代碼示例
- Java Matcher region(int, int)用法及代碼示例
- Java ZoneOffset ofHoursMinutes(int, int)用法及代碼示例
- Java Guava IntMath pow(int b, int k)用法及代碼示例
- Java Guava IntMath gcd(int a, int b)用法及代碼示例
- Java Guava LongMath binomial(int n, int k)用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 How to convert a String to an Int in Java?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。