Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import sun.security.x509.OtherName;
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static String InputPath(){
- String path;
- System.out.println("Введите имя файла, из которого хотите считать данные:");
- Scanner in = new Scanner(System.in);
- path = in.nextLine();
- path = path + ".txt";
- return path;
- }
- public static String ReadingFromFile() {
- String str ="";
- String path = InputPath();
- try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
- str = br.readLine();
- System.out.println(str);
- } catch (FileNotFoundException e) {
- System.out.println("Файл не найден.");
- } catch (IOException e) {
- System.out.println("Ошибка доступа к файлу.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return str;
- }
- static void SaveMassInFile(){
- String outputFileName;
- String str = "";
- boolean result = ChackingBrackets(str);
- Scanner in = new Scanner(System.in);
- System.out.println("Введите имя файла в который хотите вывести данные: ");
- outputFileName = in.nextLine();
- outputFileName = outputFileName + ".txt";
- File newFile = new File(outputFileName);
- try {
- boolean created = newFile.createNewFile();
- if(created)
- System.out.println("Файл успешно создан.");
- }
- catch(IOException ex){
- System.out.println(ex.getMessage());
- }
- try (FileWriter out = new FileWriter(outputFileName)) {
- out.write("Результат: \n");
- out.write(Boolean.toString(result));
- } catch (IOException e) {
- System.out.println("Ошибка доступа к файлу.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static boolean ChackingBrackets(String str){
- int counter = 0;
- boolean result = true;
- boolean isCorrect = true;
- str = ReadingFromFile();
- int i = 0;
- while((str.charAt(i) != '(') && (str.charAt(i) != '[') && (str.charAt(i) != '{')){
- if ((str.charAt(i) == ')') || (str.charAt(i) == ']') || (str.charAt(i) == '}')){
- System.out.println("False");
- isCorrect = false;
- }
- i++;
- }
- if (isCorrect) {
- isCorrect = true;
- for (i = 0; i < str.length(); i++) {
- do {
- if (counter > -1) {
- if (str.charAt(i) == '(' || str.charAt(i) == '[' || str.charAt(i) == '{')
- counter++;
- System.out.println(counter);
- if (str.charAt(i) == ')' || str.charAt(i) == ']' || str.charAt(i) == '}')
- counter--;
- System.out.println(counter);
- } else {
- isCorrect = false;
- System.out.println("ошибка");
- }
- }while (isCorrect) ;
- }
- if (counter == 0)
- result = true;
- else
- result = false;
- System.out.println(result);
- }
- return result;
- }
- public static void main(String[] args) {
- SaveMassInFile();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement