Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- static Scanner scConsole = new Scanner(System.in);
- static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- static String getLineFromFile() {
- String str = null;
- Scanner file = getInputFileLocation();
- boolean isIncorrect = false;
- do {
- try {
- str = file.nextLine();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Enter natural number");
- }
- if ((!isIncorrect) && (str.length() == 0)) {
- System.out.println("The line is empty");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- static int findBalance(String str){
- int flag = 0;
- int i = 1;
- while ((flag > -1) & (i < str.length())){
- if (str.charAt(i) == '('){
- flag++;
- }
- if (str.charAt(i) == ')'){
- flag--;
- }
- i++;
- }
- return flag;
- }
- static void printToConsole(int flag){
- if (flag == 0){
- System.out.println("В данном предложении баланс скобок соблюдён.");
- } else {
- System.out.println("Баланс скобок в данном предложении нарушен");
- }
- }
- static String getLineFromConsole () {
- String str = null;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- str = scConsole.nextLine();
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if (str.length() == 0) {
- System.out.println("You entered an empty line!\nRepeat enter");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- static void outputToFile(int flag) {
- PrintWriter out = getOutputFileLocation();
- if (flag == 0){
- out.println("В данном предложении баланс скобок соблюдён.");
- } else {
- out.println("Баланс скобок в данном предложении нарушен");
- }
- out.close();
- }
- static int chooseInput () {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- public static void main (String[]args){
- String str;
- System.out.println("Проверить, имеется ли в заданном тексте баланс открывающих и закрывающих скобок.");
- int chosenInput = chooseInput();
- System.out.println("Введите строку");
- if (chosenInput == 0) {
- str = getLineFromFile();
- } else {
- str = getLineFromConsole();
- }
- int flag = findBalance(str);
- System.out.println("Результат:");
- printToConsole(flag);
- outputToFile(flag);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement