Advertisement
sfrsnyz

NArhov YAP 7

May 21st, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. public class Main {
  11. public static void main(String[] args) throws FileNotFoundException {
  12. PrintWriter pr1=new PrintWriter(new File("First.txt"));
  13. PrintWriter pr2=new PrintWriter(new File("Second.txt"));
  14. PrintWriter pr3=new PrintWriter(new File("Third.txt"));
  15. MyClass.write(() -> { //поиск самой длинной фамилии в списке
  16. Scanner s=new Scanner(new File("In.txt"));
  17. int maxLength=0;
  18. String str=" ";
  19. while (s.hasNext()){
  20. String val=s.next();
  21. if(val.length()>maxLength){
  22. maxLength=val.length();
  23. str=val;
  24. }
  25. }
  26. pr1.println("Самая длинная фамилия в списке - "+str);
  27. pr1.close();
  28. });
  29. MyClass.write(() -> { //поиск самой короткой фамилии в списке
  30. Scanner s=new Scanner(new File("In.txt"));
  31. int minLength=1000;
  32. String str=" ";
  33. while (s.hasNext()){
  34. String val=s.next();
  35. if(val.length()<minLength){
  36. minLength=val.length();
  37. str=val;
  38. }
  39. }
  40. pr2.println("Самая длинная фамилия в списке - "+str);
  41. pr2.close();
  42. });
  43. MyClass.write(() -> { //Сортировка фамилий
  44. Scanner s=new Scanner(new File("In.txt"));
  45. List<String> list=new ArrayList<>();
  46. while (s.hasNext()){
  47. list.add(s.next());
  48. }
  49. Collections.sort(list);
  50. for(String str:list) {
  51. pr3.println(str);
  52. }
  53. pr3.close();
  54. });
  55.  
  56. }
  57. }
  58.  
  59. /////////////
  60.  
  61.  
  62. import java.io.FileNotFoundException;
  63.  
  64. public interface My {
  65. void reading() throws FileNotFoundException;
  66. }
  67.  
  68. /////////
  69.  
  70. import java.io.FileNotFoundException;
  71.  
  72. public class MyClass {
  73. public static void write(My my) throws FileNotFoundException {
  74. my.reading();
  75. }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement