java - Find Bugs - 不查找错误

标签 java eclipse findbugs

我最近在 eclipse 中设置了查找错误以查看它生成的报告。我将所有设置设置为尽可能敏感。如果我创建一个写入文件的小应用程序并且不关闭流,它会选择它,这一切都很好。

然而,使用一个已经编写的项目,我们没有几个错误,特别是在输出中,我们根本没有错误(就查找错误而言)

我想知道是否有人可以通过他们的版本运行它并报告我是否发现错误设置不正确或者实际上它是否找不到错误?

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class SpellUtil {

    private final static String teenSpelling[] = {"Zero", "One", "Two", "Three",
        "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven",
        "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
        "Seventeen", "Eighteen", "Nineteen"};

    private final static String centSpelling[] = {"Twenty", "Thirty", "Forty",
        "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    private final static String suffixSpelling[] = {
        "", // Dummy! no level 0 (added for nicer indexing in code)
        "", // Nothing for level 1
        " Thousand, ", " Million, ", " Billion, ", " Trillion, ", " Quadrillion, ",
        " Quintillion, "};



    public static String spell(int number) {

        int rem, placeIndicator = 1;
        boolean isNegative = false;
        List<String> spelling = new ArrayList<String>();

        if (number < 0) {
            isNegative = true;
            number = Math.abs(number);
        }

        while (number > 0) {
            rem = number % 1000;
            number = number / 1000;

            spelling.add(suffixSpelling[placeIndicator]);

            try {
                spelling.add(spellBelow1000(rem));
            } catch (SpellingException e) {
                System.out.println(e.getMessage());
            }

            placeIndicator++;
        }

        StringBuilder sb = new StringBuilder();
        if (isNegative) sb.append("Minus ");
        for (int i = spelling.size() - 1; i >= 0; i--) {
            sb.append(spelling.get(i));
        }

        return sb.toString();
    }

    private static String spellBelow1000(int number) throws SpellingException {

        if (number < 0 || number >= 1000)
            throw new SpellingException("Expecting a number between 0 and 999: " + number);

        if (number < 20) {
            // if number is a teen,
            // find it in teen table and return its equivalent text (word).
            return teenSpelling[number];
        } else if (number < 100) {
            // otherwise, if it is a cent,
            // find the most (div) and least (rem) significant digits (MSD/LSD)
            int div = (int) number / 10;
            int rem = (int) number % 10;

            if (rem == 0) {
                // if LSD is zero, return the cent key word directly (like
                // fifty).
                return centSpelling[div-2];
            } else {
                // otherwise, return the text as cent-teen (like fifty-one)
                return centSpelling[div-2] + "-" + teenSpelling[rem];
            }
        } else {
            // otherwise, it is a mil;
            // find it's MSD and remaining cent.
            int div = number / 100;
            int rem = (int) number % 100;  // TODO will findbugs detect unnecessary (int)?

            // Prepare the mil prefix:
            String milText = teenSpelling[div] + " Hundred";

            // decide whether to append the cent tail or not.
            if (rem == 0) {
                // if it does have a non-zero cent, that's it.
                // return the mil prefix, for example three hundred:
                return milText;
            } else {
                // otherwise, spell the cent and append it to mil prefix.
                // (now, rem is a cent).
                // For example, three Hundred and Sixty-Four:
                return milText + " and " + spellBelow1000(rem);
            }
        }
    }
}

最佳答案

您希望在此行中找到错误:

int rem = (int) number % 100;  // TODO will findbugs detect unnecessary (int)?

是错误的,因为 % 操作的结果通常不是整数。

CC++ 中,余数运算符只接受整数操作数,但 在 Java 中,它也接受 float 操作数。 这表示 double x = 8.2 % 4; 等语句在 Java 中非常有效,结果可能是非整数值。 (在本例中为 0.1999999999999993)

请参阅 Java 语言规范 here .

关于java - Find Bugs - 不查找错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12873045/

相关文章:

java - Findbugs 说 "HTTP parameter directly written to HTTP header"

java - Findbugs 问题与 "Boxing/unboxing to parse a primitive"和 Integer.valueOf(String)

java - 背压时 Intent 未清除, "on create"中的项目不断堆叠。安卓

java - 在网页中嵌入仅查看 Word 文档

android - Eclipse to Android 在部署时显示每个类的应用程序图标

java - 使用 GridLayout 两个按钮之间的额外空格

eclipse中带有启动参数的Java程序

java - 以编程方式获取惰性 JPA 字段

java - 当光标放在图表上时显示图表的值

java - 当根项目不包含任何类时,Sonar/findbugs 失败