Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws IOException {
- String str;
- str = inputInProgram();
- outputFromProgram(str);
- }
- private static boolean isMakeChoice() {
- System.out.println("Введите 1 для ввода с консоли или 2, чтобы использовать файл.");
- Scanner in = new Scanner(System.in);
- boolean correct;
- String choiceNum;
- do {
- choiceNum = in.nextLine();
- correct = !choiceNum.equals("1") && !choiceNum.equals("2");
- if (correct) {
- System.out.println("Вы можете ввести только 1 или 2, повторите попытку:");
- }
- } while (correct);
- boolean choice = false;
- if (choiceNum.equals("1")) {
- choice = true;
- }
- return choice;
- }
- private static String inputInProgram() throws IOException {
- boolean isInputMethod = isMakeChoice();
- String str;
- if (isInputMethod) {
- str = inputFromConsole();
- } else {
- str = inputFromFile();
- }
- return str;
- }
- private static String inputFromConsole() {
- System.out.println("Введите вашу строку:");
- Scanner arrayIn = new Scanner(System.in);
- String str;
- boolean correct;
- do {
- str = arrayIn.nextLine();
- correct = isIncorrectInput(str);
- if (correct) {
- System.out.println("Ваша строка не содержит скобок, введите строку со скобками:");
- correct = true;
- }
- } while (correct);
- return str;
- }
- private static String inputFromFile() throws IOException {
- Scanner arrayIn = new Scanner(System.in);
- System.out.println("Введите путь к файлу:");
- String str = "";
- boolean correct;
- do {
- String filePath = arrayIn.nextLine();
- try {
- BufferedReader in = new BufferedReader(new FileReader(filePath));
- str = in.readLine();
- in.close();
- correct = isIncorrectInput(str);
- if (correct) {
- System.out.println("Ваш файл не содержит скобок, откуройте файл со скобками:");
- correct = true;
- }
- } catch (FileNotFoundException fnf) {
- System.out.println("Перепроверьте введенный путь и повторите попытку:");
- correct = true;
- }
- } while (correct || isIncorrectInput(str));
- return str;
- }
- private static void outputFromProgram(String str) {
- boolean outputChoice = isMakeChoice();
- if (outputChoice) {
- showProgramAnswer(str);
- } else {
- outputInFile(str);
- }
- }
- private static void showProgramAnswer(String str) {
- System.out.print("Ваша строка: " + str);
- System.out.print("\n" + makeLogicOfProgram(isIncorrectInput(str), str));
- }
- private static void outputInFile(String str) {
- Scanner in = new Scanner(System.in);
- System.out.println("Учтите, что ваши данные должны быть записаны в первой строке.");
- System.out.println("Введите путь к файлу:");
- boolean correct = false;
- do {
- String filePath = in.nextLine();
- try {
- FileWriter output = new FileWriter(filePath);
- output.write("Ваша строка: " + str);
- output.write("line.separator");
- output.write(makeLogicOfProgram(isIncorrectInput(str), str));
- output.close();
- } catch (FileNotFoundException fnf) {
- System.out.println("Не удается найти указанный файл, повторите попытку:");
- correct = true;
- } catch (IOException ioe){
- System.out.println("Файл с таким именем уже существует, повторите попытку:");
- correct = true;
- }
- } while (correct);
- }
- private static String makeLogicOfProgram(boolean inputIsIncorrect, String str) {
- if (inputIsIncorrect) {
- System.out.println("Здесь нет скобок.");
- } else {
- char[] arrayOfBrackets = new char[1];
- arrayOfBrackets = makeBracketArray(arrayOfBrackets, str);
- if (isNoBalance(arrayOfBrackets)) {
- str = "Нет баланса.";
- }
- if (isBracketsBalanced(arrayOfBrackets)) {
- str = "Баланс имеется.";
- }
- }
- return str;
- }
- private static char[] makeBracketArray(char[] array, String str) {
- int count = 1;
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == '(') {
- array[count - 1] = '(';
- count++;
- array = addAnIndex(array);
- }
- if (str.charAt(i) == ')') {
- array[count - 1] = ')';
- count++;
- array = addAnIndex(array);
- }
- }
- return array;
- }
- private static boolean isNoBalance(char[] array) {
- boolean result = false;
- if (array.length % 2 == 0) {
- result = true;
- } else {
- for (int i = 0; i < array.length; i++) {
- if (array[0] == ')' || array[array.length - 1] == '(') {
- result = true;
- }
- }
- }
- return result;
- }
- private static boolean isBracketsBalanced(char[] array) {
- boolean result = false;
- int leftCountL = 0;
- int rightCountL = 0;
- int leftCountR = 0;
- int rightCountR = 0;
- for (int i = 0; i < (array.length / 2); i++) {
- if (array[i] == '(') {
- leftCountL++;
- }
- if (array[i] == ')') {
- rightCountL++;
- }
- }
- for (int i = (array.length / 2); i < array.length; i++) {
- if (array[i] == '(') {
- leftCountR++;
- }
- if (array[i] == ')') {
- rightCountR++;
- }
- }
- if (leftCountL == rightCountR && leftCountR == rightCountL) {
- result = true;
- }
- return result;
- }
- private static char[] addAnIndex(char[] array) {
- char[] newArray = new char[array.length + 1];
- int count = 0;
- for (int i = 0; i < array.length; i++) {
- newArray[i] = array[count];
- count++;
- }
- return newArray;
- }
- private static boolean isIncorrectInput(String str) {
- boolean result = true;
- for (int i = 0; i < str.length() && result; ) {
- if (str.charAt(i) == ')' || str.charAt(i) == '(') {
- result = false;
- } else {
- i++;
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement