将字符串转换为int - java.lang.NumberFormatException.forInputString
问题描述:
为什么这不起作用并抛出以下错误?将字符串转换为int - java.lang.NumberFormatException.forInputString
System.out.println(Integer.parseInt("A5"));
错误是:
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at DecisionTreeImpl.createTree(DecisionTreeImpl.java:321)
at DecisionTreeImpl.<init>(DecisionTreeImpl.java:59)
此外,如果这不是转换的字符串,如“A5”为整数,什么是在Java这样做是正确的方式好方法吗?
我给出一个类(和我应该不会修改它的话)是这样的:
public class DecTreeNode {
Integer label; //for
Integer attribute;
Integer parentAttributeValue; // if is the root, set to "-1"
boolean terminal;
List<DecTreeNode> children;
所以,当我实例化这个类,我需要传递给它的所有值(包括属性和标签都是字符串),所以我不知道我现在应该做什么,Integer.ParseInt让我失望!
它给出的暗示是你可能想从DecTreeNode类继承,但我不确定它是否相关!任何想法如何解决这个问题?
root= new DecTreeNode((trainingSet.labels.get(getMajority(instances, trainingSet.labels.size())),trainingSet.attributes.get(biggestEntropy), -1, TRUE);
这是我收到的错误:
The constructor DecTreeNode(String, String, int, boolean) is undefined
然而,问题是,我不能修改类DecTreeNode有一个新的构造。
这里是一个应该不被修改的完整DecTreeNode:
/**
* Possible class for internal organization of a decision tree.
* Included to show standardized output method, print().
*
* Do not modify. If you use,
* create child class DecTreeNodeImpl that inherits the methods.
*
*/
public class DecTreeNode {
Integer label; //for
Integer attribute;
Integer parentAttributeValue; // if is the root, set to "-1"
boolean terminal;
List<DecTreeNode> children;
DecTreeNode(Integer _label, Integer _attribute, Integer _parentAttributeValue, boolean _terminal) {
label = _label;
attribute = _attribute;
parentAttributeValue = _parentAttributeValue;
terminal = _terminal;
if (_terminal) {
children = null;
} else {
children = new ArrayList<DecTreeNode>();
}
}
/**
* Add child to the node.
*
* For printing to be consistent, children should be added
* in order of the attribute values as specified in the
* dataset.
*/
public void addChild(DecTreeNode child) {
if (children != null) {
children.add(child);
}
}
}
这里的TrainingSet类:
public class DataSet {
public List<String> labels = null; // ordered list of class labels
public List<String> attributes = null; // ordered list of attributes
public Map<String, List<String> > attributeValues = null; // map to ordered discrete values taken by attributes
public List<Instance> instances = null; // ordered list of instances
private final String DELIMITER = ","; // Used to split input strings
答
如果标签永远是一个字符,那么解决方法很简单:
System.out.println(Integer.parseInt("A5".substring(1)));
答
由于我们不知道有多少个字母(1个或更多),我会亲自去这样的事情,消除所有非数字:
String labeledNumber="A5"
String str = labeledNumber.replaceAll("\\D+","");
System.out.println(Integer.parseInt(str));
这将通过类似的标签也行:“ABC12”,“A-1232”等
答
它告诉你“的构造DecTreeNode(字符串, String,int,boolean)未定义“,因为您的类DecTreeNode没有定义具有这些数据类型的构造函数。您可以创建DecTreeNodeImpl类,扩展DecTreeNode(如DecTreeNode类中的注释所示),并实现需要String类型参数的所有方法/构造函数。
因为A5不是有效的int吗? – Alboz 2014-10-03 21:43:34
在你想它被解释为十六进制? – FatalError 2014-10-03 21:43:46
那么你将如何解决它? – 2014-10-03 21:43:57