Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws FileNotFoundException {
- PrintWriter pr1=new PrintWriter(new File("First.txt"));
- PrintWriter pr2=new PrintWriter(new File("Second.txt"));
- PrintWriter pr3=new PrintWriter(new File("Third.txt"));
- MyClass.write(() -> { //поиск самой длинной фамилии в списке
- Scanner s=new Scanner(new File("In.txt"));
- int maxLength=0;
- String str=" ";
- while (s.hasNext()){
- String val=s.next();
- if(val.length()>maxLength){
- maxLength=val.length();
- str=val;
- }
- }
- pr1.println("Самая длинная фамилия в списке - "+str);
- pr1.close();
- });
- MyClass.write(() -> { //поиск самой короткой фамилии в списке
- Scanner s=new Scanner(new File("In.txt"));
- int minLength=1000;
- String str=" ";
- while (s.hasNext()){
- String val=s.next();
- if(val.length()<minLength){
- minLength=val.length();
- str=val;
- }
- }
- pr2.println("Самая длинная фамилия в списке - "+str);
- pr2.close();
- });
- MyClass.write(() -> { //Сортировка фамилий
- Scanner s=new Scanner(new File("In.txt"));
- List<String> list=new ArrayList<>();
- while (s.hasNext()){
- list.add(s.next());
- }
- Collections.sort(list);
- for(String str:list) {
- pr3.println(str);
- }
- pr3.close();
- });
- }
- }
- /////////////
- import java.io.FileNotFoundException;
- public interface My {
- void reading() throws FileNotFoundException;
- }
- /////////
- import java.io.FileNotFoundException;
- public class MyClass {
- public static void write(My my) throws FileNotFoundException {
- my.reading();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement