Advertisement
coasterka

#4CardsFrequencies

Jun 3rd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashMap;
  4. import java.util.List;
  5. import java.util.Locale;
  6. import java.util.Map;
  7. import java.util.Scanner;
  8.  
  9. public class CardsFrequencies {
  10.  
  11.     public static void main(String[] args) {
  12.         Locale.setDefault(Locale.ROOT);
  13.         Scanner scan = new Scanner(System.in);
  14.         String[] faces = (scan.nextLine()).split("[\\W\\s]+");
  15.        
  16.         //LinkedHashMap will iterate in the order in which the entries were put into the map
  17.         LinkedHashMap<String, Integer> lhm = new LinkedHashMap<>();
  18.         List<String> faceOccurance = new ArrayList<String>();
  19.        
  20.         for (int i = 0; i < faces.length; i++) {
  21.             if (!faceOccurance.contains(faces[i])) {
  22.                 faceOccurance.add(faces[i]);
  23.             }
  24.         }
  25.        
  26.         for (int i = 0; i < faceOccurance.size(); i++) {
  27.             int counter = 0;
  28.             String currentFace = faceOccurance.get(i);
  29.  
  30.             for (int j = 0; j < faces.length; j++) {
  31.                 if (currentFace.equals(faces[j])) {
  32.                     counter++;
  33.                 }
  34.             }
  35.             lhm.put(currentFace, counter);
  36.         }
  37.        
  38.         double length = faces.length;
  39.  
  40.         for (Map.Entry<String, Integer> entry : lhm.entrySet()) {
  41.             DecimalFormat df = new DecimalFormat("#.00");
  42.             String key = entry.getKey();
  43.             int value = entry.getValue();
  44.             double occurance = (value / length) * 100;
  45.             System.out.printf("%s -> %s%s\n", key, df.format(occurance), "%");
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement