【发布时间】:2017-04-05 23:31:44
【问题描述】:
在做一个类项目,数据读入数组后出现问题。
在 Excel 电子表格中,我需要直接阅读一列,只注意每个块中的第一个数字。当我拆分列时,它是一个没有空格的连续字符串,所以我不知道如何只计算第一个数字而不是计算每个数字(就像现在的代码一样)。 示例:(11, 20, 296) 被拆分,变为“1120296”
final int[] benfordLaw = {0, 30, 18, 13, 10, 8, 7, 6, 5, 5};
int[] actualPrecent = new int[10];
int benfordScore = 0;
float[] countForNum = new float[10];
float totalCount = 0;
//This while loop will read through each line of the file
while (scan.hasNextLine()) {
// This statement reads in the next line of the file.
String line = scan.nextLine();
String[] values = line.split(",(?=(?:(?:[^\"]*+\"){2})*+[^\"]*+$)");
//12-15 is the column #'s
for (int i = 12; i < 15; ++i) {
for (int j = 0; j < values[i].length(); j++) {
char charA = values[i].charAt(j);
for (int k = 1; k <= 9; ++k){
char compare = (char)(k + '0');
if (charA == compare) {
++countForNum[k];
++totalCount;
}
}
}
}
}
【问题讨论】: