Groovy:NumberFormatException:对于输入字符串-“5.2”;
我有以下代码:Groovy:NumberFormatException:对于输入字符串-“5.2”;,groovy,Groovy,我有以下代码: static def parseString(String inputRow, Particle particle) { //input row is: //static final inputRow = "1 -5.2 3.8" def map = inputRow.split() particle.mass = Integer.parseInt(map[0]) particle.x = Integ
static def parseString(String inputRow, Particle particle) {
//input row is:
//static final inputRow = "1 -5.2 3.8"
def map = inputRow.split()
particle.mass = Integer.parseInt(map[0])
particle.x = Integer.parseInt(map[1])
particle.y = Integer.parseInt(map[2])
}
此代码引发以下错误:
java.lang.NumberFormatException: For input string: "-5.2"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.valueOf(Integer.java:582)
at RepulsionForce.parseString(RepulsionForce.groovy:13)
at RepulsionForceTest.string should be parsed into particles(RepulsionForceTest.groovy:27)
我怎样才能避免这个异常呢?哎呀,我想出来了。。。5.2不是整数
static def parseString(String inputRow, Particle particle) {
def map = inputRow.split()
particle.mass = Double.parseDouble(map[0])
particle.x = Double.parseDouble(map[1])
particle.y = Double.parseDouble(map[2])
}
哎呀,我知道了。。。5.2不是整数
static def parseString(String inputRow, Particle particle) {
def map = inputRow.split()
particle.mass = Double.parseDouble(map[0])
particle.x = Double.parseDouble(map[1])
particle.y = Double.parseDouble(map[2])
}
您可以如下图所示减少冗余线,其中
粒子是贴图:
def arr = inputRow.split()
def mass, x, y
(mass, x, y) = arr*.toDouble()
def particle = [:] << [mass: mass, x: x, y: y]
def arr=inputRow.split()
def质量,x,y
(质量,x,y)=arr*.toDouble()
def particle=[:]您可以在particle
是贴图的地方,如下图所示,减少冗余行:
def arr = inputRow.split()
def mass, x, y
(mass, x, y) = arr*.toDouble()
def particle = [:] << [mass: mass, x: x, y: y]
def arr=inputRow.split()
def质量,x,y
(质量,x,y)=arr*.toDouble()
def particle=[:]这里有一种方法。。。在完整解决方案中回答:
// setup
class Particle {
def mass
def x
def y
}
def particle = new Particle()
def inputRow = "1 -5.2 3.8"
def fieldsByIndex = [0: "mass", 1: "x", 2: "y"]
// answer
inputRow.split().eachWithIndex { val, index ->
def field = fieldsByIndex.get(index)
particle."${field}" = val.toDouble()
}
// output
println "mass : " + particle.mass
println "x : " + particle.x
println "y : " + particle.y
这里有一种方法。。。在完整解决方案中回答:
// setup
class Particle {
def mass
def x
def y
}
def particle = new Particle()
def inputRow = "1 -5.2 3.8"
def fieldsByIndex = [0: "mass", 1: "x", 2: "y"]
// answer
inputRow.split().eachWithIndex { val, index ->
def field = fieldsByIndex.get(index)
particle."${field}" = val.toDouble()
}
// output
println "mass : " + particle.mass
println "x : " + particle.x
println "y : " + particle.y
解释:您错误地尝试将双倍数解析为整数,并且它不适用,不共享相同的类型(整数!=Double),而且不起作用-因此您会得到java.lang.NumberFormatException:For input string
异常,它会明确通知您类型问题
解决方案:使用double
类将其转换为double
Double.parseDouble(map[i])
解释:您错误地尝试将双倍数解析为整数,并且它不适用,不共享相同的类型(整数!=Double),而且不起作用-因此您会得到java.lang.NumberFormatException:For input string
异常,它会明确通知您类型问题
解决方案:使用double
类将其转换为double
Double.parseDouble(map[i])
对于更多Groovynes:map[1]。toDouble()
对于更多Groovynes:map[1]。toDouble()
这非常简洁。我在哪里可以读到这句话:particle我们知道particle是一个集合吗?@michaelaster LOL,完全错过了问题中的那部分particle-particle
。谢谢我将根据地图更新答案。这非常简洁。我在哪里可以读到这句话:particle我们知道particle是一个集合吗?@michaelaster LOL,完全错过了问题中的那部分particle-particle
。谢谢我将根据地图更新答案。