为什么这不起作用并引发以下错误?
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
最佳答案
它告诉您“未定义构造函数DecTreeNode(String,String,int,boolean)”,因为您的类DecTreeNode没有使用这些数据类型定义构造函数。您可以创建一个扩展DecTreeNode的类DecTreeNodeImpl(正如DecTreeNode类的注释所建议的那样),并实现所有需要String类型参数的方法/构造函数。