[英]java.lang.NumberFormatException in System Groovy script
def ipadd = addr.hostAddress
//println ipadd
String myString = new Integer(ipadd);
def pa = new ParametersAction([new StringParameterValue('IPADDR', myString)]);
Thread.currentThread().executable.addAction(pa)
println 'Script finished! \n';
I am trying to save the ip address of the slave by adding it to System variable and pass it to next job.But when I run the job , I am getting below exception : Logs : 我正在尝试通过将奴隶的IP地址添加到系统变量中并将其传递给下一个作业来保存它的IP地址。但是当我运行该作业时,出现以下异常:日志:
Slave Machine 2: X.X.X.X
java.lang.NumberFormatException: For input string: "X.X.X.X"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.<init>(Integer.java:867)
An IPv4 address contains 3 dots in it, so it cannot be directly parsed as an Integer
. IPv4地址中包含3个点,因此无法将其直接解析为Integer
。
I suppose you are trying to convert it to the corresponding int
representing the IP 32 bits. 我想您正在尝试将其转换为代表IP 32位的相应int
。 This can be done in Java like this: 可以这样在Java中完成:
public static int ipToInt32(String ip) {
Inet4Address ipAddress;
try {
ipAddress = (Inet4Address) InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw new IllegalStateException("Cannot convert IP to bits: '" + ip + "'", e);
}
byte[] ipBytes = ipAddress.getAddress();
return ((ipBytes[0] & 0xFF) << 24)
| ((ipBytes[1] & 0xFF) << 16)
| ((ipBytes[2] & 0xFF) << 8)
| (ipBytes[3] & 0xFF);
}
You cannot cast the ipadd to an integer. 您不能将ipadd强制转换为整数。 Because it is not a valid integer. 因为它不是有效的整数。 As I see it there is no mandatory need for you to cast the ipadd to an integer. 如我所见,您无需强制将ipadd强制转换为整数。 Therefore my recommendation is to replace the line String myString = new Integer(ipadd)
with following line. 因此,我的建议是用以下行替换String myString = new Integer(ipadd)
行。
String myString = new String(ipadd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.