jwrbg

asf

Apr 11th, 2019
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package TechModule;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class p07_RegexRace {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         List<String>names= Arrays.stream(scanner.nextLine().split(", ")).collect(Collectors.toList());
  12.       Map<String,Integer>information=new HashMap<>();
  13.  
  14.       String input=scanner.nextLine();
  15.  
  16.         for (int i = 0; i <names.size() ; i++) {
  17.             information.putIfAbsent(names.get(i),0);
  18.         }
  19.         String nameRegex="(?<name>[a-zA-Z]+)";
  20.         String kmRegex="(?<km>[\\d])";
  21.  
  22.         Pattern patternName=Pattern.compile(nameRegex);
  23.         Pattern patternKm=Pattern.compile(kmRegex);
  24.         Matcher matcherName=patternName.matcher(input);
  25.         Matcher matcherKm=patternKm.matcher(input);
  26.         int sum=0;
  27.  
  28.         while(!input.equals("end of race")){
  29.              matcherName=patternName.matcher(input);
  30.              matcherKm=patternKm.matcher(input);
  31.             String b ="";
  32.             sum=0;
  33.           while(matcherName.find()){
  34.               String a = matcherName.group(1);
  35.               //String b=matcherKm.group(1);
  36.              // information.putIfAbsent(matcherName.group(1),Integer.parseInt(matcherKm.group(1)));
  37.                b +=a;
  38.           }
  39.           while(matcherKm.find()){
  40.               sum+=Integer.parseInt(matcherKm.group(1));
  41.  
  42.           }
  43.           if(information.containsKey(b)){
  44.           information.put(b,information.get(b)+sum);}
  45.  
  46.  
  47.             input=scanner.nextLine();
  48.         }
  49.  
  50.         List<String>nameZ=new ArrayList<>();
  51.  
  52.         information.entrySet().stream().sorted((a,b)->Integer.compare(b.getValue(),a.getValue()))
  53.                 .forEach(e->{nameZ.add(e.getKey());
  54.         });
  55.  
  56.         for (int i = 0; i <nameZ.size() ; i++) {
  57.             if(i==0){
  58.                 System.out.println(String.format("%dst place: %s",i+1,nameZ.get(i)));
  59.             }else if(i==1){
  60.                 System.out.println(String.format("%dnd place: %s",i+1,nameZ.get(i)));
  61.             }else if(i==2) {
  62.  
  63.             System.out.println(String.format("%drd place: %s",i+1,nameZ.get(i)));
  64.             break;}
  65.  
  66.         }
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment