2014-10-03 168 views
0

爲什麼這不起作用並拋出以下錯誤?將字符串轉換爲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 
+9

因爲A5不是有效的int嗎? – Alboz 2014-10-03 21:43:34

+3

在你想它被解釋爲十六進制? – FatalError 2014-10-03 21:43:46

+0

那麼你將如何解決它? – 2014-10-03 21:43:57

回答

0

如果標籤永遠是一個字符,那麼解決方法很簡單:

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

由於我們不知道有多少個字母(1個或更多),我會親自去這樣的事情,消除所有非數字:

String labeledNumber="A5" 

String str = labeledNumber.replaceAll("\\D+",""); 

System.out.println(Integer.parseInt(str)); 

這將通過類似的標籤也行:「ABC12」,「A-1232」等

+0

This是不正確的答案。我在各種評論中提到輸入字符串可以是anyt興。不只是十六進制。 – 2014-10-03 22:03:01

+0

@Mona Jalal先試試然後再判斷。它不是十六進制!十六進制它只是一個變量名。這將刪除字符串的所有非數字部分並保留數字! – Alboz 2014-10-03 22:05:00

+0

我更改了變量名稱:P – Alboz 2014-10-03 22:06:14

1

它告訴你「的構造DecTreeNode(字符串, String,int,boolean)未定義「,因爲您的類DecTreeNode沒有定義具有這些數據類型的構造函數。您可以創建DecTreeNodeImpl類,擴展DecTreeNode(如DecTreeNode類中的註釋所示),並實現需要String類型參數的所有方法/構造函數。