我创建了一种方法来搜索导入的文本文件中的浮点数。文件本身有超过20个浮点数,但我一直得到0作为文本中浮点数的数量。该方法是布尔值,逐字符搜索文本文件。然后main方法使用布尔值来计算浮点数
public static boolean isFloat(String token) {
if(!(token).contains(".")) return false;
if (token.startsWith("+") || token.startsWith("-")) {
for (int i = 0; i < token.length(); i++) {
if (! Character.isDigit((token).charAt(i))) {
if (!token.contains("."))
return false;
}
}
}
return true;
}
Loop within main:
while (checkFile.hasNext()) {
words = checkFile.next();
if (!notWord == isWord(words)) {
word++;
} else if (! notDigit == isInteger(digits)) {
number++;
} else if (!notFloat == isFloat(floatNum)) {
decimalNum++;
}
}