实现复合模式时更换时出现问题

  • 本文关键字:问题 复合 模式 实现 java
  • 更新时间 :
  • 英文 :


我正在研究复合模式。我有一个程序可以接受csv文件中的数据,并且必须将其打印在树中 - 类似结构。它就像....该文件有一个在根部的总统。总裁的员工 ID 将是经理的指定 ID。因此,经理的员工 ID 将是其余员工的指定 ID,包括分析师、文员、销售员。这就是树。但是,我注意到一个障碍。在行中:

if (valueOfPresident == Integer.valueOf(b[3]).intValue())

在将总裁员工 ID 与其他员工的指定 ID 进行比较时,会遇到 NULL(7839,'国王','总统',空,'17-NOV-81',5000,空,10 )而其他员工有这样的整数值 -->(7698,'布莱克','经理',7839,'1-MAY-81',2850,NULL,30)。因此,我得到了一个例外。

Exception in thread "main" java.lang.NumberFormatException: For input string: "NULL"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at com.techlabs.compositepattern.project.CompositePattern.main(CompositePattern.java:34)

如何将 NULL 值替换为 0?

程序代码是 -

    while ((line = br.readLine()) != null) {
        String[] b = line.split(",");
        if (b[3].equalsIgnoreCase("NULL")) {
            TopHierarchy tophierarchy = new TopHierarchy(b);
            valueOfPresident = (Integer.valueOf(b[0]).intValue());
            tophierarchy.showDetails();
            if (b[3].contains("NULL"))
            b[3].replace("NULL", "0000");

            if (valueOfPresident == Integer.valueOf(b[3]).intValue()) {
            TopHierarchy    tophierarchy1 = new TopHierarchy(b);
                tophierarchy.add(tophierarchy1);
                tophierarchy.showDetails();
                valueOfManager = Integer.valueOf(b[0]).intValue();

你有这个if语句:

if (b[3].equalsIgnoreCase("NULL")) {

当你在里面时,没有必要检查b[3]。您知道它是"NULL",因此您可以删除 2 if 语句。

这不执行任何操作:

b[3].replace("NULL", "0000");

字符串是不可变的。replace方法返回一个新字符串。你需要:

b[3] = b[3].replace("NULL", "0000");

但是你在一个已经检查过 b[3] 等于 "NULL" 的块中调用它。所以没有必要把if (b[3].contains("NULL"))放在前面。

最新更新