Advertisement
SmnVadik

Lab 4.4 (Java)

Mar 23rd, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4.  
  5. public class Main {
  6.     public static Scanner scanner = new Scanner(System.in);
  7.     public static Scanner fileScanner;
  8.     public static final int MAX = 50;
  9.     public static void main(String[] args) throws IOException {
  10.         control();
  11.     }
  12.  
  13.     public static void control() throws IOException {
  14.         String path;
  15.         String str;
  16.         if (selectToOpen() == 1) {
  17.             path = getPath();
  18.             fileScanner = new Scanner(path);
  19.             str = getStrFromFile(path);
  20.         } else {
  21.             str = getLine();
  22.         }
  23.         String newStr = recur(str);
  24.         if (selectToSave() == 1) {
  25.             path = getPath();
  26.             outputToFile(newStr, path);
  27.         } else {
  28.             outputToConsole(newStr);
  29.         }
  30.         scanner.close();
  31.     }
  32.  
  33.     public static String getLine() {
  34.         String str = "";
  35.         boolean isCorrect;
  36.         System.out.println("Enter line:");
  37.         do {
  38.             isCorrect = true;
  39.             try {
  40.                 str = scanner.nextLine();
  41.             } catch (Exception e) {
  42.                 isCorrect = false;
  43.             }
  44.             if (isCorrect & (str.length() > MAX)) {
  45.                 isCorrect = false;
  46.                 System.out.println("Max length = " + MAX);
  47.             }
  48.             if (isCorrect) {
  49.                 for (int i = 0; i < str.length(); i++) {
  50.                     if ((str.charAt(i) == 'a') || (str.charAt(i) == 'b') || (str.charAt(i) == 'c') || (str.charAt(i) == 'A') || (str.charAt(i) == 'B') || (str.charAt(i) == 'C')) {
  51.                     } else {
  52.                         isCorrect = false;
  53.                     }
  54.                 }
  55.                 if (!isCorrect) {
  56.                     System.out.println("Only A, B and C can be entered");
  57.                 }
  58.             }
  59.         } while (!isCorrect);
  60.         return str;
  61.     }
  62.  
  63.     public static String recur(String text) {
  64.         text = text.replace("baba", "ba");
  65.         text = text.replace("BABA", "BA");
  66.         int p = text.indexOf("baba");
  67.         int q = text.indexOf("BABA");
  68.         if(p < 0 & q < 0)
  69.             return text;
  70.         else
  71.             return recur(text);
  72.     }
  73.  
  74.     static int selectToOpen () {
  75.         int choice = 0;
  76.         boolean isCorrect;
  77.         do {
  78.             System.out.println("If you want to read data from a file - enter 1, from the console - enter 0");
  79.             isCorrect = true;
  80.             try {
  81.                 choice = Integer.parseInt(scanner.nextLine());
  82.             } catch (Exception e) {
  83.                 isCorrect = false;
  84.                 System.out.println("Check the correctness of the entered data");
  85.             }
  86.             if (isCorrect && (choice != 1 && choice != 0)) {
  87.                 isCorrect = false;
  88.                 System.out.println("Enter 1 or 0");
  89.             }
  90.         } while (!isCorrect);
  91.         return choice;
  92.     }
  93.  
  94.     static int selectToSave () {
  95.         int choice = 0;
  96.         boolean isCorrect;
  97.         do {
  98.             System.out.println("If you want to write data to a file - enter 1, if not - enter 0");
  99.             isCorrect = true;
  100.             try {
  101.                 choice = Integer.parseInt(scanner.nextLine());
  102.             } catch (Exception e) {
  103.                 isCorrect = false;
  104.                 System.out.println("Check the correctness of the entered data");
  105.             }
  106.             if (isCorrect && (choice != 1 && choice != 0)) {
  107.                 isCorrect = false;
  108.                 System.out.println("Enter 1 or 0");
  109.             }
  110.         } while (!isCorrect);
  111.         return choice;
  112.     }
  113.  
  114.     public static String getPath () {
  115.         boolean isCorrect;
  116.         String path;
  117.         do {
  118.             isCorrect = true;
  119.             System.out.println("Enter the full path to the file");
  120.             path = scanner.nextLine();
  121.             File file = new File(path);
  122.             if (!file.exists()) {
  123.                 isCorrect = false;
  124.                 System.out.println("check the correctness of the entered directory");
  125.             }
  126.         } while (!isCorrect);
  127.         return path;
  128.     }
  129.  
  130.     public static String getStrFromFile (String path) {
  131.         String str = "";
  132.         boolean isCorrect;
  133.         do {
  134.             isCorrect = true;
  135.             try {
  136.                 Scanner fileReader = new Scanner(new File(path));
  137.                 str = fileReader.nextLine();
  138.             } catch (Exception e) {
  139.                 isCorrect = false;
  140.                 System.out.println("Check the correctness of the data in the file");
  141.                 path = getPath();
  142.             }
  143.             if (isCorrect && str.length() > MAX) {
  144.                 isCorrect = false;
  145.                 System.out.println("check the correctness of the data in the file");
  146.                 path = getPath();
  147.             }
  148.         } while (!isCorrect);
  149.         return str;
  150.     }
  151.  
  152.     public static void outputToFile(String res, String path) throws IOException {
  153.         FileWriter writer = new FileWriter(path);
  154.         writer.write("New line = " + res);
  155.         writer.close();
  156.         System.out.println("Data saved successfully");
  157.     }
  158.  
  159.     public static void outputToConsole(String res) {
  160.         System.out.println("New line: " + res);
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement