Basically to explain I have a txt document that needs to be read to file http://m.uploadedit.com/bbtc/1515402294116.txt
基本上解释我有一个txt文档,需要读取文件http://m.uploadedit.com/bbtc/1515402294116.txt
How would I be able to do a number count for specifically the 3rd column? For example, the first 10 numbers in the 3rd column on the txt document starting from the top are...
我怎么能特别为第3列做数字?例如,从顶部开始的txt文档第3列中的前10个数字是......
1, 1, 3, 3, 1, 1 ,1, 3, 1, 2
If I were to do a number count for the value of 3 it would be:
如果我要对值3进行数字计数,那将是:
"The number 3 occurs: 3 times"
But I have a really big sample, and I want to only include these values for the 3rd column
, would anyone be able to help me I've been stuck on this problem for a while. I'm thinking you would have to set each columns in arrays and work that way.
但是我有一个非常大的样本,我想只为第3列包含这些值,是否有人能够帮助我我已经被困在这个问题上一段时间了。我想你必须在数组中设置每个列并以这种方式工作。
2 个解决方案
#1
2
Your text file looks like tab delimited, you could read each line using BufferedReader
and extract third column each time. Number count can be easily done using streams.
您的文本文件看起来像制表符分隔,您可以使用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()));
#2
0
if every value is separated by a ',' you could use this code:
如果每个值都以','分隔,则可以使用以下代码:
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();
}
#1
2
Your text file looks like tab delimited, you could read each line using BufferedReader
and extract third column each time. Number count can be easily done using streams.
您的文本文件看起来像制表符分隔,您可以使用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()));
#2
0
if every value is separated by a ',' you could use this code:
如果每个值都以','分隔,则可以使用以下代码:
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();
}