我在xstream上有一个非常奇怪的行为。我的测试代码是:
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class ConvertStringToNumber{
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
xstream.alias("person", Person.class);
Person c = (Person) xstream.fromXML("<person><code>01008</code></person>");
System.out.println(c);
}
}
class Person {
private int code;
public void setCode(int code){
this.code=code;
}
public int getCode(){
return this.code;
}
}当我使用String:<person><lastname>001008</lastname></person>作为XML输入运行这段代码时,我得到了一个NumberFormatException,同时也使用了<person><lastname>001009</lastname></person>
其他数字工作正常,例如: 001000,001007,001006,001005。
你知道问题出在哪里吗?
发布于 2013-04-17 17:45:42
当您离开pad时,如果一个数字只有一个零,java会将该数字视为八进制数。
八进制数只能包含以下数字:0、1、2、3、4、5、6和7。数字8和9不能用于八进制数。
您指定的数字是01008,因此它抛出NumberFormatException。
https://stackoverflow.com/questions/16022920
复制相似问题