日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【Java】字符串转换为数字:Integer的parseInt方法

發(fā)布時間:2023/11/30 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java】字符串转换为数字:Integer的parseInt方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java官方文檔[1]的解釋

public static int parseInt?(String s) throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be
decimal digits, except that the first character may be an ASCII minus sign ‘-’ (’\u002D’) to indicate
a negative value or an ASCII plus sign ‘+’ (’\u002B’) to indicate a positive value. The resulting
integer value is returned, exactly as if the argument and the radix 10 were given as arguments to
the parseInt(java.lang.String, int) method.
_
Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

中文翻譯

我來簡單翻譯一部分。

對于Integer類的靜態(tài)方法public static int parseInt?(String s) throws NumberFormatException
它能夠?qū)tring類型的參數(shù),解析為一個帶符號的十進制整數(shù)。字符串中的字符,必須是十進制數(shù)字,在這個十進制數(shù)字的第一個字符前,可以是ASCII字符的-或Unicode字符的'\u002D來表明這是一個負值,或者是一個ASCII字符的+或Unicode字符的'\u002B來表明這是一個正值。這個作為結(jié)果的整數(shù)值會被return,如果使用方法parseInt(java.lang.String, int)并為其傳參(String s,10)也能達到和當(dāng)前方法一樣的效果。

  • 參數(shù):
    • s: 一個要被解析的字符串,這個字符串看起來是一個整數(shù)
  • 返回值:
    • 一個整數(shù)值,它由十進制數(shù)字表示
  • 異常拋出:
    • NumberFormatException:如果字符串沒有包含可以被解析的整數(shù)

極簡解釋 & 應(yīng)用

簡而言之,我們就是要將String類型的整數(shù)轉(zhuǎn)化成int類型的整數(shù)。

字符串只允許出現(xiàn)十進制數(shù)字、正號(+)和負號(-),當(dāng)然,如果是Unicode編碼也可以,一般不會這樣用。

我們先來列舉一些正確的例子,如:

  • "100"–>100
  • "-100"–>-100
  • "+200"–>200
  • 示例代碼:

    String s = "100"; int sToInt = Integer.parseInt(s);

    但是,如果字符串出現(xiàn)了其他符號,就會拋出NumberFormatException。

    例如:

  • "1.1"
  • "abc"
  • "&%#"
  • 我們需要捕獲并且處理這個異常,給出一個示例代碼。

    public class TestSubmitException {public static void main(String[] args) {String[] salesOrders = {"1111", "28120821", "-111", "+1111", "1.11", "abc"};submitOrders(salesOrders);}public static void submitOrders(String[] salesOrders) {int[] salesOrdersInteger = new int[salesOrders.length];try {int i = 0;for (String s : salesOrders) {salesOrdersInteger[i] = Integer.parseInt(s);i++;}} catch (NumberFormatException e) { e.printStackTrace();}for (int a : salesOrdersInteger) {System.out.println(a);}} }

    運行結(jié)果是

    java.lang.NumberFormatException: For input string: "1.11"at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.base/java.lang.Integer.parseInt(Integer.java:652)at java.base/java.lang.Integer.parseInt(Integer.java:770)at testCode.TestSubmitException.submitOrders(TestSubmitException.java:35)at testCode.TestSubmitException.main(TestSubmitException.java:21) 1111 28120821 -111 1111 0 0Process finished with exit code 0

    我們可以看到最后Process finished with exit code 0,說明程序,成功處理了異常并且正常退出了。

    參考資料

    [1] Java11官方文檔 Integer.parseInt(String s)

    總結(jié)

    以上是生活随笔為你收集整理的【Java】字符串转换为数字:Integer的parseInt方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。