Advertisement
Vladislav8653

laba_3_4_java

Dec 9th, 2022 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.lang.String;
  3. import java.io.*;
  4. public class Main {
  5.    private static final Scanner scan = new Scanner(System.in);
  6.    private static final char[] brakes = new char[]{'[', ']', '{', '}', '(', ')'};
  7.    private static final int SQUARE_BRACKET = 0;
  8.    private static final int BRACE = 2;
  9.    private static final int BRACKET = 4;
  10.    private static final int[] counters = new int[6];
  11.  
  12.    public static void main(String[] args) {
  13.       System.out.println("Check if the given text has a balance of opening and closing brackets.");
  14.       System.out.println("Enter type of input. " + '\n' + "1 is console input, 0 is file input.");
  15.       boolean chose = choose();
  16.       String strInput;
  17.       String path;
  18.       if (chose) {
  19.          System.out.println("Input string:");
  20.          strInput = consoleInputString();
  21.       } else {
  22.          path = inputFilePath();
  23.          strInput = FileInputString(path);
  24.       }
  25.       loop(brakes, counters, strInput);
  26.       System.out.println("Enter type of output. " + '\n' + "1 is console output, 0 is file output.");
  27.       chose = choose();
  28.       if (chose) {
  29.          check(counters);
  30.       } else {
  31.          path = inputFilePath();
  32.          fileOutput(path, counters);
  33.       }
  34.    }
  35.  
  36.    private static boolean choose() {
  37.       int inputNumber = 0;
  38.       boolean isIncorrect;
  39.       final int MIN_NUM = 0;
  40.       final int MAX_NUM = 1;
  41.       do {
  42.          isIncorrect = false;
  43.          try {
  44.             inputNumber = Integer.parseInt(scan.nextLine());
  45.          } catch (Exception e) {
  46.             isIncorrect = true;
  47.             System.out.println("Please, enter a number.");
  48.          }
  49.          if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
  50.             System.out.println("You are out of input range!:");
  51.             isIncorrect = true;
  52.  
  53.          }
  54.       } while (isIncorrect);
  55.       if (inputNumber == 1)
  56.          return true;
  57.       else
  58.          return false;
  59.    }
  60.  
  61.    //console
  62.    private static String consoleInputString() {
  63.       String strInput = "";
  64.       boolean isIncorrect;
  65.       do {
  66.          isIncorrect = false;
  67.          try {
  68.             strInput = scan.nextLine();
  69.          } catch (Exception e) {
  70.             isIncorrect = true;
  71.             System.out.println("Check input data.");
  72.          }
  73.       } while (isIncorrect);
  74.       return strInput;
  75.    }
  76.  
  77.    static void countBrackets(int j, int i, char[] brakes, int[] counters, String str) {
  78.       if (str.charAt(i) == brakes[j])
  79.          counters[j]++;
  80.       if (str.charAt(i) == brakes[j + 1])
  81.          counters[j + 1]++;
  82.    }
  83.  
  84.    static void loop(char[] brakes, int[] counters, String str) {
  85.       for (int i = 0; i < str.length(); i++) {
  86.          countBrackets(SQUARE_BRACKET, i, brakes, counters, str);
  87.          if (counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET])
  88.             break;
  89.          countBrackets(BRACE, i, brakes, counters, str);
  90.          if (counters[BRACE + 1] > counters[BRACE])
  91.             break;
  92.          countBrackets(BRACKET, i, brakes, counters, str);
  93.          if (counters[BRACKET + 1] > counters[BRACKET])
  94.             break;
  95.       }
  96.    }
  97.  
  98.    static void check(int[] counters) {
  99.       if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
  100.          System.out.println("Поздравляю! Баланс есть!");
  101.       else
  102.          System.out.println("Баланса нет.");
  103.    }
  104.  
  105.    //files
  106.    private static String inputFilePath() {
  107.       String path;
  108.       boolean isIncorrect;
  109.       do {
  110.          isIncorrect = false;
  111.          System.out.println("Input file path:");
  112.          path = scan.nextLine();
  113.          File file = new File(path);
  114.          if (!file.exists()) {
  115.             System.out.println("Wrong way to file.");
  116.             isIncorrect = true;
  117.          }
  118.          if (!file.canRead() && file.exists()) {
  119.             System.out.println("Impossible to read a file.");
  120.             isIncorrect = true;
  121.          }
  122.          if (!file.canWrite() && file.canRead()) {
  123.             System.out.println("Impossible to write a file.");
  124.             isIncorrect = true;
  125.          }
  126.       } while (isIncorrect);
  127.       return path;
  128.    }
  129.  
  130.    private static String FileInputString(String path) {
  131.       String strInput = "";
  132.       boolean isIncorrect;
  133.       do {
  134.          isIncorrect = false;
  135.          try {
  136.             Scanner fileScanner = new Scanner(new File(path));
  137.             strInput = fileScanner.nextLine();
  138.          } catch (Exception q) {
  139.             isIncorrect = true;
  140.             System.out.println("Check file");
  141.             path = inputFilePath();
  142.          }
  143.       } while (isIncorrect);
  144.       return strInput;
  145.    }
  146.  
  147.    private static void fileOutput(String path, int[] counters) {
  148.       boolean isIncorrect;
  149.       do {
  150.          isIncorrect = false;
  151.          try {
  152.             FileWriter writer = new FileWriter(path);
  153.             if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
  154.                writer.write(("Congratulations! Balance is exist!"));
  155.             else
  156.                writer.write(("There is no balance there."));
  157.             writer.close();
  158.          } catch (IOException e) {
  159.             isIncorrect = true;
  160.             System.out.println("Mistake of output in file. Input path.");
  161.             path = inputFilePath();
  162.          }
  163.       } while (isIncorrect);
  164.       System.out.println("Successful output in file.");
  165.    }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement