Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.String;
- import java.io.*;
- public class Main {
- private static final Scanner scan = new Scanner(System.in);
- private static final char[] brakes = new char[]{'[', ']', '{', '}', '(', ')'};
- private static final int SQUARE_BRACKET = 0;
- private static final int BRACE = 2;
- private static final int BRACKET = 4;
- private static final int[] counters = new int[6];
- public static void main(String[] args) {
- System.out.println("Check if the given text has a balance of opening and closing brackets.");
- System.out.println("Enter type of input. " + '\n' + "1 is console input, 0 is file input.");
- boolean chose = choose();
- String strInput;
- String path;
- if (chose) {
- System.out.println("Input string:");
- strInput = consoleInputString();
- } else {
- path = inputFilePath();
- strInput = FileInputString(path);
- }
- loop(brakes, counters, strInput);
- System.out.println("Enter type of output. " + '\n' + "1 is console output, 0 is file output.");
- chose = choose();
- if (chose) {
- check(counters);
- } else {
- path = inputFilePath();
- fileOutput(path, counters);
- }
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- //console
- private static String consoleInputString() {
- String strInput = "";
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- strInput = scan.nextLine();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Check input data.");
- }
- } while (isIncorrect);
- return strInput;
- }
- static void countBrackets(int j, int i, char[] brakes, int[] counters, String str) {
- if (str.charAt(i) == brakes[j])
- counters[j]++;
- if (str.charAt(i) == brakes[j + 1])
- counters[j + 1]++;
- }
- static void loop(char[] brakes, int[] counters, String str) {
- for (int i = 0; i < str.length(); i++) {
- countBrackets(SQUARE_BRACKET, i, brakes, counters, str);
- if (counters[SQUARE_BRACKET + 1] > counters[SQUARE_BRACKET])
- break;
- countBrackets(BRACE, i, brakes, counters, str);
- if (counters[BRACE + 1] > counters[BRACE])
- break;
- countBrackets(BRACKET, i, brakes, counters, str);
- if (counters[BRACKET + 1] > counters[BRACKET])
- break;
- }
- }
- static void check(int[] counters) {
- if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
- System.out.println("Поздравляю! Баланс есть!");
- else
- System.out.println("Баланса нет.");
- }
- //files
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static String FileInputString(String path) {
- String strInput = "";
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- Scanner fileScanner = new Scanner(new File(path));
- strInput = fileScanner.nextLine();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return strInput;
- }
- private static void fileOutput(String path, int[] counters) {
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- if ((counters[SQUARE_BRACKET] == counters[SQUARE_BRACKET + 1]) && (counters[BRACE] == counters[BRACE + 1]) && (counters[BRACKET] == counters[BRACKET + 1]))
- writer.write(("Congratulations! Balance is exist!"));
- else
- writer.write(("There is no balance there."));
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Mistake of output in file. Input path.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- System.out.println("Successful output in file.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement