Ale*_*app 1 java string parseint
由于某种原因,我无法获得字符串数值转换为整数我运行的错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6327818260"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at MyDirectory.getsize(MyDirectory.java:18)
at a19010.main(a19010.java:79)
Run Code Online (Sandbox Code Playgroud)
Java结果:1
如果我的输入字符串是"6327818260",为什么它不能成为整数?我的代码是
public String getsize()
{
int intSize = Integer.parseInt(mySize);
int count = 0;
String dataType = "";
while (intSize > 1000)
{
intSize = intSize / 1000;
count++;
}
switch (count)
{
case 0:
dataType = "Bytes";
break;
case 1:
dataType = "KB";
break;
case 2:
dataType = "MB";
break;
case 3:
dataType = "GB";
break;
case 4:
dataType = "KB";
break;
}
return intSize + dataType ;
}
Run Code Online (Sandbox Code Playgroud)
mySize取自此处从文本文件中获取的字符串的一部分
public class MyDirectory
{
String myName = "" ;
String myNum = "";
String mySize = "";
public MyDirectory(String line)
{
line = line.trim();
String[] lineParts = line.split(" ");
mySize = lineParts[0];
myNum = lineParts[1];
myName = lineParts[3];
}
Run Code Online (Sandbox Code Playgroud)
线im分裂看起来像6327818260 6486 SUB-TOTAL:E:\ WIS26\LCORRES
最大可能的整数
2147483647
比您尝试解析的值小得多:
6327818260
您需要使用long或BigInteger/BigDecimal来保存该值.
对于BigInteger/BigDecimal表示基数为10的整数可以由带有字符串参数的构造函数解析.
BigDecimal bigDecimal = new BigDecimal("6327818260");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4440 次 |
| 最近记录: |