Reputation: 38155
Why would this not work and throw the following error?
System.out.println(Integer.parseInt("A5"));
Error is:
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)
Also if this is not a good method for converting the strings like "A5" to integer, what's the way to do it correctly in Java?
I am given a class (and I am supposed not to modify it at all) which is like this:
public class DecTreeNode {
Integer label; //for
Integer attribute;
Integer parentAttributeValue; // if is the root, set to "-1"
boolean terminal;
List<DecTreeNode> children;
So when I am instantiating this class all the values which I need to pass to it (including attributes and labels are all string) so I have no idea what I should do now that Integer.ParseInt is failing me!
It is given as a hint that you might want to inherit from DecTreeNode class but I was not sure if that is related at all! Any idea how to tackle this problem?
root= new DecTreeNode((trainingSet.labels.get(getMajority(instances, trainingSet.labels.size())),trainingSet.attributes.get(biggestEntropy), -1, TRUE);
Here's the error I receive :
The constructor DecTreeNode(String, String, int, boolean) is undefined
However the problem is I am not allowed to modify the class DecTreeNode to have a new constructor.
Here's the complete DecTreeNode that is supposed not to be modified:
/**
* 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);
}
}
}
Here's TrainingSet class:
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
Upvotes: 0
Views: 10550
Reputation: 502
It's telling you "The constructor DecTreeNode(String, String, int, boolean) is undefined" because your class DecTreeNode does not define a constructor with those data types. You can create a class DecTreeNodeImpl extending DecTreeNode(as the comments on the DecTreeNode class suggests) and implement all the methods/constructors that need parameters of type String.
Upvotes: 1
Reputation: 1851
Since we don't know how many letters there are (1 or more), I would personally go for something like this that removes all non-digits:
String labeledNumber="A5"
String str = labeledNumber.replaceAll("\\D+","");
System.out.println(Integer.parseInt(str));
This would work also with labels like: "abc12" "a-1232" etc.
Upvotes: 0
Reputation: 3720
If label is always one char, then solution is simple:
System.out.println(Integer.parseInt("A5".substring(1)));
Upvotes: 0