This repository was archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaba5.java
More file actions
217 lines (193 loc) · 6.54 KB
/
Copy pathLaba5.java
File metadata and controls
217 lines (193 loc) · 6.54 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.company;
import org.w3c.dom.DOMStringList;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
public class Main {
public static void Task1() {
String str;
System.out.println("Введіть рядок: ");
str = new Scanner(System.in).nextLine();
if(str.isEmpty())
{
System.out.println("Рядок пустий!!!!!!!");
}
String sentences[] = str.split("[.?!]\\s*");
final String words[][] = new String[sentences.length][];
for (int i = 0; i < sentences.length; ++i) {
words[i] = sentences[i].split("[\\p{Punct}\\s]+");
System.out.println("Речення: "+ (i+1) +"\tКількість слів: "+words[i].length);
}
System.out.println("Найдовше речення");
int maxIndex=0;
for(int i=1;i< words.length;i++)
{
if(words[maxIndex].length<words[i].length)
{
maxIndex=i;
}
}
for(int i=0;i<words[maxIndex].length;i++)
{
System.out.print(words[maxIndex][i]+" ");
}
}
public static void Task2()
{
String str;
System.out.println("Введіть назву предмету: ");
str=new Scanner(System.in).nextLine();
String fileName=str+".txt";
System.out.println(fileName);
BufferedReader br=null;
try
{
//Створення файлу
File file=new File(fileName);
if(!file.exists())
file.createNewFile();
PrintWriter pw=new PrintWriter(file);
byte n;
byte rating;
String firName;
//Запис інформації
do {
System.out.println("Прізвеще студена: "); firName= new Scanner(System.in).nextLine();
System.out.println("Оцінка:"); rating =new Scanner(System.in).nextByte();
pw.println(firName+"-"+rating);
System.out.println("1-Продовжити введення: \n 2-Зчитати данні:");
n =new Scanner(System.in).nextByte();
}while(n==1);
pw.close();
if(n==2) {
br = new BufferedReader(new FileReader(fileName));
String string;
while ((string = br.readLine()) != null) {
System.out.println(string+"\t");
}
}
else
System.out.println("(");
}catch(IOException e)
{
System.out.println("Error "+e);
}
}
static int CountWords(String[] words)
{
int countWords=0;
for(int i=0;i< words.length;i++)
{
if(words[i].length()%2!=0)
countWords++;
}
return countWords;
}
static boolean Konteins(Character a, List<Character> b)
{
for(int i=0;i<b.size();i++)
{
if(b.get(i).equals(a))
return true;
}
return false;
}
static void Chastota(String string)
{
List<Character> stringList=new ArrayList<Character>();
char[] arr=string.toCharArray();
for(int i=0;i<arr.length;i++)
{
stringList.add(arr[i]);
}
for (int i=0;i<stringList.size();i++)
{
if(stringList.get(i).equals(' '))
{
stringList.remove(i);
}
}
List <Character> backList=new ArrayList<Character>();
try {
File file = new File("Output.txt");
if (!file.exists())
file.createNewFile();
PrintWriter pw = new PrintWriter(file);
for (int i = 0; i < stringList.size(); i++) {
if (Konteins(stringList.get(i), backList) == false) {
System.out.print("Cимвол " + stringList.get(i));
int count = 0;
for (int j = 0; j < stringList.size(); j++) {
if (stringList.get(j).equals(stringList.get(i)))
count++;
}
System.out.println(": " + count);
pw.println("Cимвол " + stringList.get(i)+": "+count);
backList.add(stringList.get(i));
}
}
pw.close();
}catch(IOException e)
{
System.out.println("Помалка: "+ e);
}
}
public static void Task3()
{
BufferedReader br=null;
String inputFileName="Input.txt";
String outputFileName="Output.txt";
try {
String string=null;
String string1="";
br = new BufferedReader(new FileReader(inputFileName));
while ((string = br.readLine()) != null) {
string1+=string;
}
System.out.println(string1);
String[] words=string1.split("[ ]\\s*");
System.out.println("Кількість не парних слів: "+CountWords(words));
System.out.println("Часота повторення символів: ");
Chastota(string1);
}catch (IOException e)
{
System.out.println("Error "+e);
}
}
public static void main(String[] args) {
boolean run = true;
byte number;
Scanner num = new Scanner(System.in);
while (true) {
System.out.println("Введіть номер завдання(1-3;4-вихід)");
number=num.nextByte();
switch (number)
{
case 1:
System.out.println("Завдання №1: ");
Task1();
break;
case 2:
System.out.println("Завдання №2: ");
Task2();
break;
case 3:
System.out.println("Завдання №3: ");
Task3();
case 4:
System.out.println("Вихід! ");
return;
default:
System.out.println("Введи щераз!");
break;
}
}
}
}