Java 将字符串解析为long时出现问题-我做错了什么?

Java 将字符串解析为long时出现问题-我做错了什么?,java,parsing,long-integer,Java,Parsing,Long Integer,以下是我在主方法中使用的代码: String numbers = "12345678900"; long upc = Integer.parseInt(numbers); System.out.println(upc); 给我: Exception in thread "main" java.lang.NumberFormatException: For input string: "12345678900" at java.lang.NumberFormatException.forInpu

以下是我在主方法中使用的代码:

String numbers = "12345678900";
long upc = Integer.parseInt(numbers);
System.out.println(upc);
给我:

Exception in thread "main" java.lang.NumberFormatException: For input string: "12345678900"
at java.lang.NumberFormatException.forInputString...
at java.lang.Integer.parseInt....
at java.lang.Integer.parseInt...
at testRun.main...

我不能使用double,它们需要存储为没有小数的值。我试图将一个字符串中的数字串放入一个包含数字(无小数)的变量中。

您传递的数字超出了整数范围,即从-2147483648到2147483647。
尝试使用静态函数parseLong来解析
long
,不要使用
Integer.parseInt
。通过这种方式,您可以访问整个
long
值范围(而使用
parseInt
,您只能获得更严格的
int
值范围)。

使用
long.parseLong()

使用


您的数字足够长,无法放入整数。所以,不能强制转换为整数。您可以通过调用
Long.parseLong(number)
使用

String numbers = "12345678900";
long upc = Long.parseLong(numbers);
System.out.println(upc);

没有涉及“演员阵容”。请在发布前查看您的答案,以确保其格式符合您的预期。请尝试使用long代替integer。
long upc = Long.parseLong(numbers);
String numbers = "12345678900";
long upc = Long.parseLong(numbers);
System.out.println(upc);