Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.*;
- import java.util.regex.Pattern;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- //read the file
- File file = new File("D:\\JavaLearning\\log.txt");
- Scanner scan = new Scanner(file);
- ArrayList<String[]> fileList = new ArrayList<String[]>();
- Pattern pattern = Pattern.compile("\\s+");
- //read to the end of the file, split with spaces 1 or more using Pattern
- while (scan.hasNext()) {
- fileList.add(pattern.split(scan.nextLine()));
- }
- scan.close();
- System.out.println(Arrays.deepToString(fileList.get(0)) + "\n");
- //EXTRACT COLUMN NAME , count, sort and print most common
- ArrayList<String> columnName = extractColumn(fileList, 0);
- int[] max = countInt(columnName);
- Arrays.sort(max);
- printResult(max);
- //most common name
- String[] mostFrequentNames = countString(columnName);
- Arrays.sort(mostFrequentNames);
- }
- /**
- * Extract wanted column from the txt file
- * @param fileList - ArrayList<String[]></>
- * @param columnWanted - column int ( 0, 1, 2...)
- * @return ArrayList<String></>
- */
- public static ArrayList<String> extractColumn(ArrayList<String[]> fileList, int columnWanted){
- ArrayList<String> column = new ArrayList<String>();
- for(int i = 0; i < fileList.size(); i++){
- column.add(fileList.get(i)[columnWanted]);
- }
- return column;
- }
- /**
- * Count occurrences
- * @param column ArrayList<String></>
- * @return - int array
- */
- //count frequency, return int
- public static int[] countInt(ArrayList<String> column){
- int counter = 0;
- String name = "";
- ArrayList<String> names = filesNoRepeat(column);
- int[] max = new int[names.size()];
- max[0] = Collections.frequency(column, names.get(0));
- String[] mostFrequent = new String[names.size()];
- for (int i = 0; i < names.size(); i++) {
- name = names.get(i);
- counter = Collections.frequency(column, name);
- if(max[i] < counter){
- max[i] = counter;
- mostFrequent[i] = name;
- }
- }
- return max;
- }
- //count frequency, return String
- public static String[] countString(ArrayList<String> column){
- int counter = 0;
- String name = "";
- ArrayList<String> names = filesNoRepeat(column);
- int[] max = new int[names.size()];
- max[0] = Collections.frequency(column, names.get(0));
- String[] mostFrequent = new String[names.size()];
- for (int i = 0; i < names.size(); i++) {
- name = names.get(i);
- counter = Collections.frequency(column, name);
- if(max[i] < counter){
- max[i] = counter;
- mostFrequent[i] = name;
- }
- }
- return mostFrequent;
- }
- public static ArrayList<String> filesNoRepeat(ArrayList<String> list){
- ArrayList<String> removedDuplicatesList = new ArrayList<String>();
- for(String element : list){
- if(!removedDuplicatesList.contains(element)) removedDuplicatesList.add(element);
- }
- return removedDuplicatesList;
- }
- public static void printResult(int[] tries){
- for (int i = 1; i <= 10; i++) {
- System.out.println(tries[tries.length - i]);
- }
- }
- public static void printResult(String[] tries){
- for (int i = 1; i <= 10; i++) {
- System.out.println(tries[tries.length - i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement