Advertisement
dimipan80

Exam 4. Couples Frequency

Sep 10th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3.  
  4. public class _4_CouplesFrequency {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         Scanner scan = new Scanner(System.in);
  9.         String inputLine = scan.nextLine().trim();
  10.  
  11.         String[] inputNums = inputLine.split(" ");
  12.  
  13.         LinkedHashMap<String, Integer> frequenciesMap = new LinkedHashMap<>();
  14.         String couple;
  15.         Integer times;
  16.         for (int i = 0; i + 1 < inputNums.length; i++) {
  17.             couple = inputNums[i] + " " + inputNums[i + 1];
  18.  
  19.             times = frequenciesMap.get(couple);
  20.             if (times == null) {
  21.                 times = 0;
  22.             }
  23.  
  24.             frequenciesMap.put(couple, times + 1);
  25.         }
  26.  
  27.         float totalOccurrences = inputNums.length - 1;
  28.         printTheFrequenciesOfAllCouples(frequenciesMap, totalOccurrences);
  29.     }
  30.  
  31.     private static void printTheFrequenciesOfAllCouples(
  32.             LinkedHashMap<String, Integer> frequenciesMap, float allOccurrences) {
  33.         float percentage;
  34.         for (String couple : frequenciesMap.keySet()) {
  35.             System.out.print(couple + " -> ");
  36.             percentage = (frequenciesMap.get(couple) * 100) / allOccurrences;
  37.             System.out.printf("%.2f", percentage);
  38.             System.out.println("%");
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement