Advertisement
Boyan5

oooo k

Nov 14th, 2021
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8.  
  9. public class FileExampleClass {
  10.     private ArrayList<ArrayList<String>> data;
  11.  
  12.     // CONSTRUCTOR : DEFAULT
  13.     public FileExampleClass() {
  14.         data = new ArrayList<>();
  15.     }
  16.  
  17.  
  18.     // CONSTRUCTOR : WITH PARAMS :
  19.     // 1. ЧЕТЕ ФАЙЛА РЕД ПО РЕД (temp);
  20.     // 2. РАЯЗДЕЛЯ ГО НА ДУМИ И ГИ ЗАПИСВА В СПИСЪК (words); // СПИСЪК С ДУМИТЕ
  21.     // 3. ЗАПИСВА СПИСЪКА ОТ ДУМИ В СПИСЪКА (data) // СПИСЪК С РЕДОВЕТЕ
  22.     public FileExampleClass(String fileName) {
  23.         this();
  24.         try {
  25.             Scanner sc = new Scanner(new File(fileName), "UTF-8");
  26.             while (sc.hasNextLine()) {
  27.                 String temp = sc.nextLine();
  28.                 ArrayList<String> words = new ArrayList<>();
  29.                 words.addAll(Arrays.asList(temp.split(" ")));
  30.                 data.add(words);
  31.             }
  32.             sc.close();
  33.  
  34.             // ОБРАБОТВА ЕКСЕПШЪН ЧЕ НЕ Е НАМЕРИЛ ФАЙЛА
  35.         } catch (FileNotFoundException e) {
  36.             // TODO Auto-generated catch block
  37.             e.printStackTrace();
  38.         }
  39.     }
  40.  
  41.  
  42.  
  43.     //METHOD : РАЗМЯНА НА ДУМИТЕ :
  44.     // ПОЛЗВА : x - номер на единия ред; y-номер на другия ред;
  45.     // a - номер на едната дума; b- номер на другата дума;
  46.  
  47.     public void exchangeWords(int x, int y, int a, int b) {
  48.  
  49.         // ВАЛИДИРА ЕДИНИЯ РЕД
  50.         if (x < 1 || x > data.size()) {
  51.             System.out.println("x is not valid row number");
  52.             return;
  53.         }
  54.         // ВАЛИДИРА ДРУГИЯ РЕД
  55.         if (y < 1 || y > data.size()) {
  56.             System.out.println("y is not valid row number");
  57.             return;
  58.         }
  59.  
  60.         // ВАЛИДИРА ДУМИТЕ ОТ ЕДИНИЯ РЕД
  61.         if (a < 1 || a > data.get(x-1).size() || a > data.get(y-1).size()) {
  62.             System.out.println("a is not valid word number");
  63.             return;
  64.         }
  65.         // ВАЛИДИРА ДУМИТЕ ОТ ДРУГИЯ РЕД
  66.         if (b < 1 || b > data.get(x-1).size() || b > data.get(y-1).size()) {
  67.             System.out.println("b is not valid word number");
  68.             return;
  69.         }
  70.  
  71.         // РАЗМЯНА НА МЕСТАТА НА ДМИТЕ
  72.         String temp = data.get(x-1).get(a-1);           // НАЧИН ЗА ИЗВИКВАНЕ НА ЕЛЕМЕНТ ОТ СПЪСЪК (words),
  73.         // КОЙТО Е В ДРУГ СПИСЪК (data)
  74.         data.get(x-1).set( a-1, data.get(y-1).get(b-1) ); // ТРОЙНО ПРЕХВЪРЛЯНЕ : едната дума в темп,
  75.         // другата дума в предната
  76.         // и накрая темп (буфера) втората ;
  77.         data.get(y-1).set(b-1, temp);
  78.     }//METHOD - END : РАЗМЯНА НА ДУМИТЕ.
  79.  
  80.  
  81.  
  82.     // METHOD : РАЗМЯНА НА РЕДОВЕТЕ (!!! НЕ СЕ ПОЛЗВА В MAIN !!!):
  83.     public void exchangeRows(int x, int y) {
  84.         if (x < 1 || x > data.size()) { // "МАСИВА" С РЕДОВЕТЕ
  85.             System.out.println("x is not valid row number");
  86.             return;
  87.         }
  88.         if (y < 1 || y > data.size()) { // "МАСИВА" С РЕДОВЕТЕ
  89.             System.out.println("y is not valid row number");
  90.             return;
  91.         }
  92.  
  93.         int start = Math.min(x - 1, y - 1);
  94.         int end = Math.max(x - 1, y - 1);
  95.  
  96. //      String temp = data.get(start);
  97. //      data.set(start, data.get(end));
  98. //      data.set(end, temp);
  99.     }//METHOD - END: РАЗМЯНА НА РЕДОВЕТЕ.
  100.  
  101.  
  102.     // METHOD : ЗА ЗАПИСВАНЕ В НОВ ФАЙЛ :
  103.     public void writeToFile(String fileName) {
  104.         try {
  105.             FileWriter out = new FileWriter(fileName);
  106.             out.write(toString());
  107.             out.close();
  108.         } catch (IOException e) {
  109.             // TODO Auto-generated catch block
  110.             e.printStackTrace();
  111.         }
  112.     }//METHOD - END: ЗА ЗАПИСВАНЕ В НОВ ФАЙЛ.
  113.  
  114.  
  115.  
  116.     @Override // ОВЪРРАЙД - ПРЕНАПИСВАНЕ НА МЕТОД В JAVA;
  117.     public String toString() {
  118.         StringBuilder str = new StringBuilder();
  119.  
  120.         for (ArrayList<String> element : data) { //FOR-EACH ЗА СПЪСЪКА С РЕДОВЕТЕ (КОЙТО МНОГО ПРИЛИЧА НА ФОР-ЦИКЪЛ)
  121.  
  122.             for (String s : element) { // //FOR-EACH ЗА СПЪСЪКА С ДУМИТЕ
  123.                 str.append(s + " ");
  124.             }
  125.             str.append("\n");
  126.         }
  127.         return str.toString();
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. import java.util.Scanner;
  134.  
  135. public class FileExampleMain {
  136.     public static void main(String[] args) {
  137.         // TODO Auto-generated method stub
  138.  
  139.         // СЪЗДАВАМЕ ОБЕКТ fc
  140.         FileExampleClass fc = new FileExampleClass("C:\\Users\\Boyan\\Desktop\\problem.txt");
  141.         System.out.println(fc);
  142.  
  143.         Scanner sc = new Scanner(System.in);
  144.  
  145.         System.out.print("x=");
  146.         int x = sc.nextInt();
  147.  
  148.         System.out.print("y=");
  149.         int y = sc.nextInt();
  150.  
  151.         System.out.print("a=");
  152.         int a = sc.nextInt();
  153.  
  154.         System.out.print("b=");
  155.         int b = sc.nextInt();
  156.  
  157. //          fc.exchangeRows(x, y);
  158.         fc.exchangeWords(x, y, a, b);
  159.         System.out.println(fc);
  160.         fc.writeToFile("Problem.out.txt");
  161.     }
  162. }
  163.  
  164.  
  165.  
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement