Basically to explain I have a txt document that needs to be read to file 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...
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:
"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.
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.
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()));