-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3_new.java
More file actions
33 lines (30 loc) · 1.39 KB
/
Lab3_new.java
File metadata and controls
33 lines (30 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
/* 1429 % 3 = 1 => C3 = 1 => Тип змінних - StringBuffer
1429 % 17 = 1 => C17 = 1 => Дія з рядком - Вивести всі речення заданого тексту в порядку зростання кількості слів у них.
*/
public class Lab3_new {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the text.Then press Enter.Then press Ctrl+D.");
StringBuffer text = new StringBuffer(scan.nextLine());
while (scan.hasNext()) {
StringBuffer a = new StringBuffer(scan.nextLine());
text.append(a);
}
String[] sentences = text.toString().split("(?<=[.?!]{1,3}) +");
String[][] splittedSentences = new String[sentences.length][];
for (int i = 0; i < sentences.length; i++) {
splittedSentences[i] = sentences[i].split("(( - )| +)");
}
Arrays.sort(splittedSentences, Comparator.comparingInt(o -> o.length));
StringBuffer finalText = new StringBuffer();
for (String[] splittedSentence : splittedSentences) {
for (String s : splittedSentence) {
finalText.append(s).append(" ");
}
}
System.out.println(finalText);
}
}