Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package dip107;
  2.  
  3. import java.util.Scanner;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.util.ArrayList;
  10. import java.util.Comparator;
  11.  
  12. public class Ld5_02_181rdb232 {
  13. private static ArrayList<String> createLines(ArrayList<String> words) {
  14. ArrayList<String> result = new ArrayList<>();
  15.  
  16. StringBuilder stringBuilder = new StringBuilder();
  17. for (String word : words) {
  18. stringBuilder.append(word).append(" ");
  19. if (word.length() != 0 && word.charAt(word.length() - 1) == '.') {
  20. result.add(stringBuilder.toString().trim());
  21. stringBuilder = new StringBuilder();
  22. }
  23. }
  24.  
  25. return result;
  26. }
  27.  
  28. private static ArrayList<String> getFileContents(String fileName) {
  29. try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
  30. ArrayList<String> allWords = new ArrayList<>();
  31. String line;
  32. while ((line = reader.readLine()) != null) {
  33. String[] words = line.split("\\s+");
  34.  
  35. for (String word : words)
  36. allWords.add(word);
  37. }
  38.  
  39. return createLines(allWords);
  40. } catch (IOException ex) {
  41. System.out.println(ex.getMessage());
  42. return null;
  43. }
  44. }
  45.  
  46. private static void modifyFileContents(ArrayList<String> lines) {
  47. String longestLine = lines.stream().max(Comparator.comparing(String::length)).get();
  48.  
  49. for (int i = 0; i < lines.size(); i++) {
  50. String currentLine = lines.get(i);
  51. if (currentLine.compareTo(longestLine) != 0) {
  52. StringBuilder stringBuilder = new StringBuilder(currentLine);
  53. while (stringBuilder.length() != longestLine.length()) {
  54. stringBuilder.insert(0, " ");
  55. }
  56.  
  57. lines.set(i, stringBuilder.toString());
  58. }
  59. }
  60. }
  61.  
  62. private static void writeToFile(String fileName, ArrayList<String> lines) {
  63. System.out.println("result:");
  64. try (PrintWriter out = new PrintWriter(new FileWriter(fileName))) {
  65. for (int i=0;i<lines.size();i++) {
  66. out.println(lines.get(i));
  67. System.out.println(lines.get(i));
  68. }
  69. }
  70. catch (Exception ex) {
  71. System.out.println(ex.getMessage());
  72. }
  73. }
  74.  
  75. public static void main(String[] args) {
  76. System.out.println("Niklāvs Māris Arbidāns 12 181RDB232");
  77. System.out.println("Roberts Jānis Gaigals 11 181RDB179");
  78.  
  79. Scanner sc = new Scanner(System.in);
  80. System.out.print("input file name: ");
  81. String fileName = sc.nextLine();
  82.  
  83. ArrayList<String> lines = getFileContents(fileName);
  84. if (lines != null) {
  85. modifyFileContents(lines);
  86. writeToFile(fileName, lines);
  87. }
  88.  
  89. sc.close();
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement