Advertisement
madopew

Untitled

Feb 25th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package laba4_2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.util.Scanner;
  9.  
  10. //accbaaaaabcabcbabacbacba
  11. //accbbacbacba
  12.  
  13. public class Main {
  14.     public static void main(String[] args) {
  15.         System.out.println("This program changes string to specific rules");
  16.         boolean isConsole = getConsole();
  17.         String line = isConsole ? input() : inputFile();
  18.         line = change(line);
  19.         System.out.printf("Changed string:\n%s\n", line);
  20.         if(!isConsole)
  21.             saveToFile(line);
  22.     }
  23.    
  24.     static Scanner scanner = new Scanner(System.in);
  25.    
  26.     static boolean getConsole() {
  27.         String line;
  28.         while(true) {
  29.             System.out.println("Do you want to input from console? (y/n):");
  30.             line = scanner.nextLine();
  31.             line.toLowerCase();
  32.             if(line.equals("y")) return true;
  33.             if(line.equals("n")) return false;
  34.         }
  35.     }
  36.    
  37.     static String input() {
  38.         String line;
  39.         do {
  40.             System.out.println("Input string that contains only ABC's:");
  41.             line = scanner.nextLine();
  42.             line.toLowerCase();
  43.         } while(!checkInput(line));
  44.         return line;
  45.     }
  46.    
  47.     static String inputFile() {
  48.         String line;
  49.         boolean incorrect;
  50.         do {
  51.             incorrect = false;
  52.             System.out.println("Input file's location:");
  53.             line = scanner.nextLine();
  54.             try (BufferedReader br = new BufferedReader(new FileReader(new File(line)))) {
  55.                 line = br.readLine();
  56.                 line.toLowerCase();
  57.                 incorrect = !checkInput(line);
  58.             } catch (Exception e) {
  59.                 incorrect = true;
  60.                 System.out.println("Cannot open the file");
  61.             }
  62.         } while (incorrect);
  63.         return line;
  64.     }
  65.    
  66.     static boolean checkInput(String line) {
  67.         for(char c : line.toCharArray()) {
  68.             if(c != 'a' && c != 'b' && c != 'c') {
  69.                 System.out.println("String should contain only ABC's");
  70.                 return false;
  71.             }
  72.         }
  73.         return true;
  74.     }
  75.    
  76.     static String change(String line) {
  77.         line = deletePattern(line, "aaaa", 0, 4, "");
  78.         line = deletePattern(line, "baba", 0, 2, "");
  79.         line = deletePattern(line, "abc", 0, 3, "");
  80.         return line;
  81.     }
  82.    
  83.     static String deletePattern(String line, String pattern, int i, int amount, String output) {
  84.         String _output = output;
  85.         if(i == line.length())
  86.             return _output;
  87.         if(line.charAt(i) == pattern.charAt(0) && isExact(line, pattern, i+1, 1))
  88.             i += amount - 1;
  89.         else
  90.             _output += line.charAt(i);
  91.         return deletePattern(line, pattern, i + 1, amount, _output);
  92.     }
  93.    
  94.     static boolean isExact(String line, String pattern, int i1, int i2) {
  95.         if(i1 == line.length() && i2 != pattern.length())
  96.             return false;
  97.         if(i2 == pattern.length())
  98.             return true;
  99.         if(line.charAt(i1) == pattern.charAt(i2))
  100.             return isExact(line, pattern, i1+1, i2+1);
  101.         return false;
  102.     }
  103.    
  104.     static void saveToFile(String line) {
  105.         System.out.println("Input location of a file:");
  106.         String location;
  107.         location = scanner.nextLine();
  108.         try(BufferedWriter bw = new BufferedWriter(new FileWriter(new File(location), false))) {
  109.             bw.append(line);
  110.             System.out.println("File is saved");
  111.         } catch (Exception e) {
  112.             System.out.println("Cannot open the file");
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement