Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Main {
- public static Scanner scanner = new Scanner(System.in);
- public static Scanner fileScanner;
- public static final int MAX = 50;
- public static void main(String[] args) throws IOException {
- control();
- }
- public static void control() throws IOException {
- String path;
- String str;
- if (selectToOpen() == 1) {
- path = getPath();
- fileScanner = new Scanner(path);
- str = getStrFromFile(path);
- } else {
- str = getLine();
- }
- String newStr = recur(str);
- if (selectToSave() == 1) {
- path = getPath();
- outputToFile(newStr, path);
- } else {
- outputToConsole(newStr);
- }
- scanner.close();
- }
- public static String getLine() {
- String str = "";
- boolean isCorrect;
- System.out.println("Enter line:");
- do {
- isCorrect = true;
- try {
- str = scanner.nextLine();
- } catch (Exception e) {
- isCorrect = false;
- }
- if (isCorrect & (str.length() > MAX)) {
- isCorrect = false;
- System.out.println("Max length = " + MAX);
- }
- if (isCorrect) {
- for (int i = 0; i < str.length(); i++) {
- if ((str.charAt(i) == 'a') || (str.charAt(i) == 'b') || (str.charAt(i) == 'c') || (str.charAt(i) == 'A') || (str.charAt(i) == 'B') || (str.charAt(i) == 'C')) {
- } else {
- isCorrect = false;
- }
- }
- if (!isCorrect) {
- System.out.println("Only A, B and C can be entered");
- }
- }
- } while (!isCorrect);
- return str;
- }
- public static String recur(String text) {
- text = text.replace("baba", "ba");
- text = text.replace("BABA", "BA");
- int p = text.indexOf("baba");
- int q = text.indexOf("BABA");
- if(p < 0 & q < 0)
- return text;
- else
- return recur(text);
- }
- static int selectToOpen () {
- int choice = 0;
- boolean isCorrect;
- do {
- System.out.println("If you want to read data from a file - enter 1, from the console - enter 0");
- isCorrect = true;
- try {
- choice = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Check the correctness of the entered data");
- }
- if (isCorrect && (choice != 1 && choice != 0)) {
- isCorrect = false;
- System.out.println("Enter 1 or 0");
- }
- } while (!isCorrect);
- return choice;
- }
- static int selectToSave () {
- int choice = 0;
- boolean isCorrect;
- do {
- System.out.println("If you want to write data to a file - enter 1, if not - enter 0");
- isCorrect = true;
- try {
- choice = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Check the correctness of the entered data");
- }
- if (isCorrect && (choice != 1 && choice != 0)) {
- isCorrect = false;
- System.out.println("Enter 1 or 0");
- }
- } while (!isCorrect);
- return choice;
- }
- public static String getPath () {
- boolean isCorrect;
- String path;
- do {
- isCorrect = true;
- System.out.println("Enter the full path to the file");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- isCorrect = false;
- System.out.println("check the correctness of the entered directory");
- }
- } while (!isCorrect);
- return path;
- }
- public static String getStrFromFile (String path) {
- String str = "";
- boolean isCorrect;
- do {
- isCorrect = true;
- try {
- Scanner fileReader = new Scanner(new File(path));
- str = fileReader.nextLine();
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Check the correctness of the data in the file");
- path = getPath();
- }
- if (isCorrect && str.length() > MAX) {
- isCorrect = false;
- System.out.println("check the correctness of the data in the file");
- path = getPath();
- }
- } while (!isCorrect);
- return str;
- }
- public static void outputToFile(String res, String path) throws IOException {
- FileWriter writer = new FileWriter(path);
- writer.write("New line = " + res);
- writer.close();
- System.out.println("Data saved successfully");
- }
- public static void outputToConsole(String res) {
- System.out.println("New line: " + res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement