【问题标题】:Converting String to int--java.lang.NumberFormatException.forInputString将 String 转换为 int--java.lang.NumberFormatException.forInputString
【发布时间】:2025-12-02 21:45:01
【问题描述】:

为什么这不起作用并抛出以下错误?

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?
  • 你希望它被解释为十六进制吗?
  • 那么你将如何解决它?
  • @Mona Jalal 您不能将非整数转换为整数。删除 5 之前的 A 哈哈 :)
  • 你的问题好像XY problem。您是否介意与我们分享更多上下文,例如该字符串来自何处,为什么要将其转换为整数(您将如何使用它)?

标签: java string parseint


【解决方案1】:

它告诉您“构造函数 DecTreeNode(String, String, int, boolean) 未定义”,因为您的类 DecTreeNode 没有定义具有这些数据类型的构造函数。您可以创建一个扩展 DecTreeNode 的类 DecTreeNodeImpl(正如 DecTreeNode 类上的 cmets 所建议的那样)并实现所有需要字符串类型参数的方法/构造函数。

【讨论】:

    【解决方案2】:

    如果标签总是一个字符,那么解决方法很简单:

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

    【讨论】:

      【解决方案3】:

      由于我们不知道有多少个字母(1 个或更多),我个人会选择这样的方法来删除所有非数字:

      String labeledNumber="A5"
      
      String str = labeledNumber.replaceAll("\\D+","");
      
      System.out.println(Integer.parseInt(str));
      

      这也适用于以下标签:“abc12”“a-1232”等。

      【讨论】:

      • 这不是正确的答案。我在各种 cmets 中提到输入字符串可以是任何东西。不仅仅是十六进制。
      • @Mona Jalal 先试一试再判断。这不是十六进制! hex 它只是一个变量名。这将删除字符串的所有非数字部分并保留数字!
      • 我改了变量名:P
      • @MonaJalal 这是一个很大的变化,我会看看 :)。无论如何,这解决了你一开始问的问​​题..
      • 你上面写的,我相信B5和A5本身并没有区别!对吗?