Advertisement
Guest User

2_555

a guest
Nov 11th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. package com.company;
  2. import sun.security.x509.OtherName;
  3.  
  4. import java.io.*;
  5. import java.util.Scanner;
  6. public class Main {
  7.  
  8. public static String InputPath(){
  9. String path;
  10. System.out.println("Введите имя файла, из которого хотите считать данные:");
  11. Scanner in = new Scanner(System.in);
  12. path = in.nextLine();
  13. path = path + ".txt";
  14. return path;
  15. }
  16. public static String ReadingFromFile() {
  17. String str ="";
  18. String path = InputPath();
  19. try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
  20. str = br.readLine();
  21. System.out.println(str);
  22. } catch (FileNotFoundException e) {
  23. System.out.println("Файл не найден.");
  24. } catch (IOException e) {
  25. System.out.println("Ошибка доступа к файлу.");
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. return str;
  30. }
  31. static void SaveMassInFile(){
  32. String outputFileName;
  33. String str = "";
  34. boolean result = ChackingBrackets(str);
  35. Scanner in = new Scanner(System.in);
  36. System.out.println("Введите имя файла в который хотите вывести данные: ");
  37. outputFileName = in.nextLine();
  38. outputFileName = outputFileName + ".txt";
  39. File newFile = new File(outputFileName);
  40. try {
  41. boolean created = newFile.createNewFile();
  42. if(created)
  43. System.out.println("Файл успешно создан.");
  44. }
  45. catch(IOException ex){
  46. System.out.println(ex.getMessage());
  47. }
  48. try (FileWriter out = new FileWriter(outputFileName)) {
  49. out.write("Результат: \n");
  50. out.write(Boolean.toString(result));
  51. } catch (IOException e) {
  52. System.out.println("Ошибка доступа к файлу.");
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. public static boolean ChackingBrackets(String str){
  58. int counter = 0;
  59. boolean result = true;
  60. boolean isCorrect = true;
  61. str = ReadingFromFile();
  62. int i = 0;
  63. while((str.charAt(i) != '(') && (str.charAt(i) != '[') && (str.charAt(i) != '{')){
  64. if ((str.charAt(i) == ')') || (str.charAt(i) == ']') || (str.charAt(i) == '}')){
  65. System.out.println("False");
  66. isCorrect = false;
  67. }
  68. i++;
  69. }
  70. if (isCorrect) {
  71. isCorrect = true;
  72. for (i = 0; i < str.length(); i++) {
  73. do {
  74. if (counter > -1) {
  75. if (str.charAt(i) == '(' || str.charAt(i) == '[' || str.charAt(i) == '{')
  76. counter++;
  77. System.out.println(counter);
  78. if (str.charAt(i) == ')' || str.charAt(i) == ']' || str.charAt(i) == '}')
  79. counter--;
  80. System.out.println(counter);
  81. } else {
  82. isCorrect = false;
  83. System.out.println("ошибка");
  84. }
  85. }while (isCorrect) ;
  86. }
  87. if (counter == 0)
  88. result = true;
  89. else
  90. result = false;
  91. System.out.println(result);
  92. }
  93. return result;
  94. }
  95.  
  96. public static void main(String[] args) {
  97. SaveMassInFile();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement