DanikYakush

Task41

Jul 12th, 2022
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Comparator;
  3. import java.util.Scanner;
  4.  
  5.  
  6.  
  7. public class Task41 {
  8.  
  9.     static Scanner sc = new Scanner(System.in);
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         String s = "A pilot  in the 123 awakes one morning to see, standing before him. The most extraordinary little fellow.\n"
  14.                 + "Please, asks the word, draw me a sheep! \n"
  15.                 + "And the pil345345345ot realizes that when life's events are too difficult to understand,\n"
  16.                 + "345 is no word but to succumb to their mysteries? He pulls out pencil and paper. And 234 begins this wise!\n";
  17.  
  18.         System.out.println(s);
  19.  
  20.         String[] paragraphs = s.split("[\\r\\n]");
  21.  
  22.     //  Pattern pattern = Pattern.compile("[\\r\\n]");
  23.     //  Matcher matcher = pattern.matcher(s);
  24.  
  25.         System.out.println();
  26.  
  27.         menu();
  28.         menuchoise(sc.nextInt(), paragraphs);
  29.  
  30.     }
  31.  
  32.     public static void menu() {
  33.         System.out.println("hello, it's 1,2,3 application");
  34.         System.out.println("1 for sort paragraphs");
  35.         System.out.println("2 for sort words in sentenses by words length");
  36.         System.out.println("3 for sort words in sentenses by count of chars ");
  37.     }
  38.  
  39.     public static void menuchoise(int choise, String[] paragraphs) {
  40.  
  41.         switch (choise) {
  42.  
  43.         case (1):
  44.             Arrays.sort(paragraphs);
  45.             System.out.println(Arrays.toString(paragraphs));
  46.             menu();
  47.             menuchoise(sc.nextInt(), paragraphs);
  48.  
  49.         case (2):
  50.  
  51.             Comparator<String> stringLengthComparator = new StringLengthSort();
  52.             for (int i = 0; i < paragraphs.length; i++) {
  53.                 String[] sentenses = paragraphs[i].split("[!?.]");
  54.                 String[] words = new String[0];
  55.  
  56.                 words = new String[paragraphs[i].length()];
  57.  
  58.                 for (int j = 0; j < sentenses.length; j++) {
  59.                     words = sentenses[j].split(" ");
  60.                     Arrays.sort(words, stringLengthComparator);
  61.                     System.out.println(Arrays.toString(words));
  62.                 }
  63.             }
  64.  
  65.             menu();
  66.             menuchoise(sc.nextInt(), paragraphs);
  67.  
  68.         case (3):
  69.            
  70.             System.out.println("need some char for search");
  71.             String input = sc.next();
  72.            
  73.            
  74.            
  75.  
  76.         }
  77.  
  78.     }
  79.  
  80. }
  81.  
  82. class StringLengthSort implements Comparator<String> {
  83.     @Override
  84.     public int compare(String o1, String o2) {
  85.         if (o1.length() > o2.length()) {
  86.             return 1;
  87.         } else {
  88.             if (o1.length() < o2.length()) {
  89.                 return -1;
  90.             } else {
  91.                 return 0;
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment