0

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
14
  • 9
    Because A5 is not a valid int? Commented Oct 3, 2014 at 21:43
  • 3
    As in you want it to be interpreted as hex? Commented Oct 3, 2014 at 21:43
  • So how would you tackle it? Commented Oct 3, 2014 at 21:43
  • 1
    @Mona Jalal you cannot turn a non int to an int. Remove the A before the 5 lol :) Commented Oct 3, 2014 at 21:44
  • 4
    Your question seems like XY problem. Would you mind sharing with us more context like where does this string come from, why you want to convert it to integer (how are you going to use it)? Commented Oct 3, 2014 at 21:51

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If label is always one char, then solution is simple:

System.out.println(Integer.parseInt("A5".substring(1)));

Comments

0

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.

5 Comments

This is not correct answer. I mentioned in various comments that input string can be anything. Not just hex.
@Mona Jalal first try it and then judge. It's not hex! hex it's just a variable name. This will remove all the non numeric part of the string and keep the numbers!
I changed the variable name :P
@MonaJalal that's a big change, I'll give it a look :). Anyway this addresses what you asked at the beginning..
What you have written above, I believe doesn't differentiate between B5 and A5 per se! Right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.