Integer.parseInt(s, radix)

本文详细介绍了Java内置函数parseInt的用法及常见错误情况,包括如何将字符串转换为指定基数的整数,以及在不同场景下可能出现的异常情况。通过具体的例子,帮助开发者正确理解和应用此函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • parseInt
    public static int parseInt(String s,int radix)
                        throws NumberFormatException
    Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), 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.

    An exception of type NumberFormatException is thrown if any of the following situations occurs:

    • The first argument is null or is a string of length zero.
    • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
    • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
    • The value represented by the string is not a value of type int.

    Examples:

     parseInt("0", 10) returns 0
     parseInt("473", 10) returns 473
     parseInt("+42", 10) returns 42
     parseInt("-0", 10) returns 0
     parseInt("-FF", 16) returns -255
     parseInt("1100110", 2) returns 102
     parseInt("2147483647", 10) returns 2147483647
     parseInt("-2147483648", 10) returns -2147483648
     parseInt("2147483648", 10) throws a NumberFormatException
     parseInt("99", 8) throws a NumberFormatException
     parseInt("Kona", 10) throws a NumberFormatException
     parseInt("Kona", 27) returns 411787
     
    Parameters:
    s - the String containing the integer representation to be parsed
    radix - the radix to be used while parsing s.
    Returns:
    the integer represented by the string argument in the specified radix.
    Throws:
    NumberFormatException - if the String does not contain a parsable int.  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值