爲什麼這不起作用並拋出以下錯誤?將字符串轉換爲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
因爲A5不是有效的int嗎? – Alboz 2014-10-03 21:43:34
在你想它被解釋爲十六進制? – FatalError 2014-10-03 21:43:46
那麼你將如何解決它? – 2014-10-03 21:43:57