Reputation: 1157
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?
Upvotes: 0
Views: 562
Reputation: 145
public class test {
public static void main(String args[]) {
String ipString="662 20 15 176";
String[] ipArray = ipString.split(" ");
String ip = "";
for (String part : ipArray){
if (part != null){
ip += part;
}
}
ip = ip.trim();
Long ipInt = Long.parseLong(ip);
System.out.println(""+ipInt);
}
}
Upvotes: 0
Reputation: 301
6,622,015,176 This number is out of range of int.You should use long instead of int which will provide you a large range.
Upvotes: 0