Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save milten15/8b6b87ed395132a52fa890586af99360 to your computer and use it in GitHub Desktop.
Save milten15/8b6b87ed395132a52fa890586af99360 to your computer and use it in GitHub Desktop.
Чтение из файла, сортировка и вывод
/* Сортировка четных чисел из файла
1. Ввести имя файла с консоли.
2. Прочитать из него набор чисел.
3. Вывести на консоль только четные, отсортированные по возрастанию.
Пример ввода:
5
8
11
3
2
10
Пример вывода:
2
8
10
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Solution
{
public static void main(String[] args) throws IOException
{
// напишите тут ваш код
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader readFile = new BufferedReader(new FileReader(reader.readLine()));
ArrayList<Integer> arrayList = new ArrayList<>();
while ((readFile.ready()))
{
int data = Integer.parseInt(readFile.readLine());
if(data % 2 == 0){
arrayList.add(data);
}
}
reader.close();
readFile.close();
Collections.sort(arrayList);
for (int i : arrayList){
System.out.println(i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment