在java中将字符串格式化为整数

在java中将字符串格式化为整数,java,numberformatexception,Java,Numberformatexception,我面临以下问题: 我将获得与temp值相似或长度更大的值: public class NumberFormat { public static void main(String arg[]){ Integer numValue = null; String temp="5474151538110135"; numValue=Integer .parseInt(temp.trim()); System.out.println("-->

我面临以下问题:

我将获得与temp值相似或长度更大的值:

 public class NumberFormat {
     public static void main(String arg[]){
     Integer numValue = null;
     String temp="5474151538110135";
     numValue=Integer
    .parseInt(temp.trim());
     System.out.println("--> "+numValue);

}
}
请提供解决方案

Exception in thread "main" java.lang.NumberFormatException: For input string: "5474151538110135"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:60)
    at java.lang.Integer.parseInt(Integer.java:473)
    at java.lang.Integer.parseInt(Integer.java:511)
    at com.filetransfer.August.NumberFormat.main(NumberFormat.java:10)

5474151538110135
大于。如果输入数字可能显著增加,请改用
Long.parseLong
biginger

Long numValue = Long.parseLong(temp.trim());

5474151538110135
大于。如果输入数字可能显著增加,请改用
Long.parseLong
biginger

Long numValue = Long.parseLong(temp.trim());

可能是因为该值大于最大int值2147483647

System.out.println(Integer.MAX_VALUE);
您应该将其解析为Long,最大值为9223372036854775807

System.out.println(Long.MAX_VALUE);
像这样

Long numValue = null;
  String temp="5474151538110135";
  numValue=Long
      .parseLong(temp.trim());

可能是因为该值大于最大int值2147483647

System.out.println(Integer.MAX_VALUE);
您应该将其解析为Long,最大值为9223372036854775807

System.out.println(Long.MAX_VALUE);
像这样

Long numValue = null;
  String temp="5474151538110135";
  numValue=Long
      .parseLong(temp.trim());

我建议使用BigInteger来避免错误

BigInteger类的优点

Integer是原语类型int的包装器。包装器类基本上用于以下情况:您希望将原语视为ex的对象,尝试在只接受对象类型的方法中传递int值。在这种情况下,您希望将原语int值包装在object类型的包装器Integer中。要了解Integer的具体优势,我建议您看看Sun提供的Integer api

现在来看BigInteger,您将在处理非常大的数字的计算中使用它。BigInteger的使用是在安全性方面的,通常用于密钥规范。有关BigInteger的更多信息,请查看以下链接


我希望这些信息对你有帮助

我建议使用BigInteger来避免错误

BigInteger类的优点

Integer是原语类型int的包装器。包装器类基本上用于以下情况:您希望将原语视为ex的对象,尝试在只接受对象类型的方法中传递int值。在这种情况下,您希望将原语int值包装在object类型的包装器Integer中。要了解Integer的具体优势,我建议您看看Sun提供的Integer api

现在来看BigInteger,您将在处理非常大的数字的计算中使用它。BigInteger的使用是在安全性方面的,通常用于密钥规范。有关BigInteger的更多信息,请查看以下链接


我希望这些信息对你有帮助

字符串对于整数来说太大字符串对于整数来说太大