Advertisement
remote87

Reading file and sorting info

Dec 7th, 2020
564
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.*;
  6. import java.util.regex.Pattern;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) throws FileNotFoundException {
  11.  
  12.         //read the file
  13.         File file = new File("D:\\JavaLearning\\log.txt");
  14.         Scanner scan = new Scanner(file);
  15.  
  16.         ArrayList<String[]> fileList = new ArrayList<String[]>();
  17.         Pattern pattern = Pattern.compile("\\s+");
  18.  
  19.         //read to the end of the file, split with spaces 1 or more using Pattern
  20.         while (scan.hasNext()) {
  21.             fileList.add(pattern.split(scan.nextLine()));
  22.         }
  23.  
  24.         scan.close();
  25.         System.out.println(Arrays.deepToString(fileList.get(0)) + "\n");
  26.  
  27.         //EXTRACT COLUMN NAME , count, sort and print most common
  28.         ArrayList<String> columnName = extractColumn(fileList, 0);
  29.         int[] max = countInt(columnName);
  30.         Arrays.sort(max);
  31.         printResult(max);
  32.  
  33.         //most common name
  34.         String[] mostFrequentNames = countString(columnName);
  35.         Arrays.sort(mostFrequentNames);
  36.     }
  37.  
  38.     /**
  39.      * Extract wanted column from the txt file
  40.      * @param fileList - ArrayList<String[]></>
  41.      * @param columnWanted - column int ( 0, 1, 2...)
  42.      * @return ArrayList<String></>
  43.      */
  44.     public static ArrayList<String> extractColumn(ArrayList<String[]> fileList, int columnWanted){
  45.         ArrayList<String> column = new ArrayList<String>();
  46.         for(int i = 0; i < fileList.size(); i++){
  47.             column.add(fileList.get(i)[columnWanted]);
  48.         }
  49.         return column;
  50.     }
  51.  
  52.     /**
  53.      * Count occurrences
  54.      * @param column ArrayList<String></>
  55.      * @return - int array
  56.      */
  57.     //count frequency, return int
  58.     public static int[] countInt(ArrayList<String> column){
  59.         int counter = 0;
  60.         String name = "";
  61.  
  62.         ArrayList<String> names = filesNoRepeat(column);
  63.         int[] max = new int[names.size()];
  64.         max[0] = Collections.frequency(column, names.get(0));
  65.         String[] mostFrequent = new String[names.size()];
  66.  
  67.         for (int i = 0; i < names.size(); i++) {
  68.             name = names.get(i);
  69.             counter = Collections.frequency(column, name);
  70.  
  71.             if(max[i] < counter){
  72.                 max[i] = counter;
  73.                 mostFrequent[i] = name;
  74.             }
  75.         }
  76.  
  77.         return max;
  78.     }
  79.  
  80.     //count frequency, return String
  81.     public static String[] countString(ArrayList<String> column){
  82.         int counter = 0;
  83.         String name = "";
  84.  
  85.         ArrayList<String> names = filesNoRepeat(column);
  86.         int[] max = new int[names.size()];
  87.         max[0] = Collections.frequency(column, names.get(0));
  88.         String[] mostFrequent = new String[names.size()];
  89.  
  90.         for (int i = 0; i < names.size(); i++) {
  91.             name = names.get(i);
  92.             counter = Collections.frequency(column, name);
  93.  
  94.             if(max[i] < counter){
  95.                 max[i] = counter;
  96.                 mostFrequent[i] = name;
  97.             }
  98.         }
  99.  
  100.         return mostFrequent;
  101.     }
  102.  
  103.     public static ArrayList<String> filesNoRepeat(ArrayList<String> list){
  104.         ArrayList<String> removedDuplicatesList = new ArrayList<String>();
  105.         for(String element : list){
  106.             if(!removedDuplicatesList.contains(element)) removedDuplicatesList.add(element);
  107.         }
  108.         return removedDuplicatesList;
  109.     }
  110.  
  111.     public static void printResult(int[] tries){
  112.         for (int i = 1; i <= 10; i++) {
  113.             System.out.println(tries[tries.length - i]);
  114.         }
  115.     }
  116.  
  117.     public static void printResult(String[] tries){
  118.         for (int i = 1; i <= 10; i++) {
  119.             System.out.println(tries[tries.length - i]);
  120.         }
  121.     }
  122.  
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement