我最近在 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);
}
}
}
}