如何计算 Java 中某些列的 txt 文件中整数出现的次数?
How can I count the number of integer occurences from a txt file from certain columns in Java?
基本上是为了解释我有一个txt文档需要读取到文件http://m.uploadedit.com/bbtc/1515402294116.txt
我怎样才能专门计算第 3 列的数字?比如txt文档第3列的前10个数字是...
1, 1, 3, 3, 1, 1 ,1, 3, 1, 2
如果我要对值 3 进行计数,它将是:
"The number 3 occurs: 3 times"
但我有一个非常大的样本,我只想包含 3rd column
的这些值,有人能帮助我吗?我已经被这个问题困了一段时间了。我认为您必须在数组中设置每一列并以这种方式工作。
您的文本文件看起来像制表符分隔,您可以使用 BufferedReader
读取每一行并每次提取第三列。使用流可以轻松完成数字计数。
File file = new File(PATH_TO_TXT);
ArrayList<Integer> storage = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] arr = line.split("\t");
storage.add(Integer.valueOf(arr[2].trim()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<Integer, Long> occurrences = storage.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()));
如果每个值都由“,”分隔,您可以使用此代码:
BufferedReader br = null;
int counter = 0;
try {
br = new BufferedReader(new FileReader("testfile.txt"));
String line;
// the column you want to get:
int column = 3;
// the value you want to search
String value = "3";
while ((line = br.readLine()) != null) {
int first = 0;
int last = line.indexOf(",");
//get index of the column
for (int i = 0; i < column - 1; i++) {
first = last;
last = line.indexOf(",", last + 1);
}
// get the value of the column
String column3value = line.substring(first + 1, last);
// counter +1 for every value
if (column3value.equals(value)) {
counter += 1;
}
}
System.out.println(counter);
} catch (IOException e) {
e.printStackTrace();
}
基本上是为了解释我有一个txt文档需要读取到文件http://m.uploadedit.com/bbtc/1515402294116.txt
我怎样才能专门计算第 3 列的数字?比如txt文档第3列的前10个数字是...
1, 1, 3, 3, 1, 1 ,1, 3, 1, 2
如果我要对值 3 进行计数,它将是:
"The number 3 occurs: 3 times"
但我有一个非常大的样本,我只想包含 3rd column
的这些值,有人能帮助我吗?我已经被这个问题困了一段时间了。我认为您必须在数组中设置每一列并以这种方式工作。
您的文本文件看起来像制表符分隔,您可以使用 BufferedReader
读取每一行并每次提取第三列。使用流可以轻松完成数字计数。
File file = new File(PATH_TO_TXT);
ArrayList<Integer> storage = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
String[] arr = line.split("\t");
storage.add(Integer.valueOf(arr[2].trim()));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<Integer, Long> occurrences = storage.stream()
.collect(Collectors.groupingBy(e -> e, Collectors.counting()));
如果每个值都由“,”分隔,您可以使用此代码:
BufferedReader br = null;
int counter = 0;
try {
br = new BufferedReader(new FileReader("testfile.txt"));
String line;
// the column you want to get:
int column = 3;
// the value you want to search
String value = "3";
while ((line = br.readLine()) != null) {
int first = 0;
int last = line.indexOf(",");
//get index of the column
for (int i = 0; i < column - 1; i++) {
first = last;
last = line.indexOf(",", last + 1);
}
// get the value of the column
String column3value = line.substring(first + 1, last);
// counter +1 for every value
if (column3value.equals(value)) {
counter += 1;
}
}
System.out.println(counter);
} catch (IOException e) {
e.printStackTrace();
}