Search code examples
javastringintegerparseintnumberformatexception

Converting String to int in Java and getting a NumberFormatException, can't figure out why


ipString is a String representation of an IP address with spaces instead of dots.

String[] ipArray = ipString.split(" ");
String ip = "";
for (String part : ipArray){
    if (part != null){
        ip += part;
    }
}
ip = ip.trim();
int ipInt = Integer.parseInt(ip); // Exception is thrown here.

Exception in thread "main" java.lang.NumberFormatException: For input string: "6622015176". Could someone explain why this exception is being thrown?


Solution

  • int is primitive data type and it's range is : -2,147,483,648 to 2,147,483,647

    6,622,015,176 is out of int range.