Java Integer.parseInt() 不适用于大数

发布于 2024-12-19 20:26:20 字数 650 浏览 2 评论 0 原文

我有以下简单的代码片段,旨在检测给定的 IPv4 地址确实只有数字值(即在点被剥离之后):

import edu.gcc.processing.exceptions.net.IPAddressNumericException;

//Get the IP address
  String address = "239.255.255.255";

//Check to see if this is a number      
  try {
    String IPNumbers = address.replace(".", "");
    Integer.parseInt(IPNumbers);            
  } catch (NumberFormatException e) {
    System.out.print(e.getMessage());
  }

由于某种原因,NumberFormatException 被触发,我收到此错误:

For input string: "239255255255"

有人可以帮我理解这一点吗? parseInt() 方法适用于较小的数字,例如 127001

谢谢您的宝贵时间。

I have the following simple piece of code which is intended to detect that a given IPv4 address indeed only has numeric values (that is after the dots have been stripped):

import edu.gcc.processing.exceptions.net.IPAddressNumericException;

//Get the IP address
  String address = "239.255.255.255";

//Check to see if this is a number      
  try {
    String IPNumbers = address.replace(".", "");
    Integer.parseInt(IPNumbers);            
  } catch (NumberFormatException e) {
    System.out.print(e.getMessage());
  }

For some reason, the NumberFormatException is fired, and I get this error:

For input string: "239255255255"

Could someone please help me understand this? The parseInt() method works on smaller numbers, such as 127001.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

七婞 2024-12-26 20:26:20

尝试使用Long.parseLong(IPNumbers)

try using Long.parseLong(IPNumbers)

苏别ゝ 2024-12-26 20:26:20

为什么不使用正则表达式,或者使用 split(".") 将其分解为各个组成部分?

除了需要仅由数字组成之外,还有其他限制。例如:

666.666.666.666

这可以很好地解析,但是……不太可能是 IP。

将其分成几个部分可以让您确定 (a) 它有四个部分,以及 (b) 每个部分在 IP 地址的上下文中实际上都有意义。

您还可以使用 InetAddress 实现,或 InetAddressValidator 来自 Apache Commons

Why not use a regular expression, or break it down into its components by using split(".")?

There are other limitations besides needing to consist solely of digits. For example:

666.666.666.666

This will parse just fine, but is an... unlikely IP.

Breaking it up into its parts lets you determine (a) that it has four parts, and (b) that each part actually makes sense in the context of an IP address.

You could also use an InetAddress implementation, or InetAddressValidator from Apache Commons.

尬尬 2024-12-26 20:26:20

对于非常大的整数,您可能需要使用 < strong>BigInteger 类(BigDecimal),因为值可能超出整数的限制。

整数限制:

Minimum : -2147483648
Maximum :  2147483647

使用 BigInteger:

string s = "239255255255";
BigInteger yourNumber = new BigInteger(s);

For very large integers you may want to use the BigInteger class (or BigDecimal), as the values may exceed the limits of Integer.

Integer Limits:

Minimum : -2147483648
Maximum :  2147483647

Using BigInteger:

string s = "239255255255";
BigInteger yourNumber = new BigInteger(s);
筱武穆 2024-12-26 20:26:20

239255255255 太大,无法由 Integer 容纳。尝试使用 BigInteger 或 BigDecimal。

239255255255 is too big to be held by an Integer. Try using a BigInteger or a BigDecimal.

北渚 2024-12-26 20:26:20

Integer 的范围是 -2,147,483,6482,147,483,647,上面提到的数字不包含在这些范围内,所以使用 long long 基元类型的范围介于两者之间-9,223,372,036,854,775,8089,223,372,036,854,775,807

因此,对于您的情况,最好使用 Long.parseLong() 函数将如此大的数字转换为 long 类型。

Range of an Integer is -2,147,483,648 to 2,147,483,647, the number you have above mentioned is not included in these range, so use long , the range of long primitive type is in between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.

So for your case its better to use Long.parseLong() function to convert such large number to long type.

墨落成白 2024-12-26 20:26:20

从整数开始的逻辑步骤是 long< /a>

The logical step up from an integer is a long

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文