Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laba4_2;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.Scanner;
- //accbaaaaabcabcbabacbacba
- //accbbacbacba
- public class Main {
- public static void main(String[] args) {
- System.out.println("This program changes string to specific rules");
- boolean isConsole = getConsole();
- String line = isConsole ? input() : inputFile();
- line = change(line);
- System.out.printf("Changed string:\n%s\n", line);
- if(!isConsole)
- saveToFile(line);
- }
- static Scanner scanner = new Scanner(System.in);
- static boolean getConsole() {
- String line;
- while(true) {
- System.out.println("Do you want to input from console? (y/n):");
- line = scanner.nextLine();
- line.toLowerCase();
- if(line.equals("y")) return true;
- if(line.equals("n")) return false;
- }
- }
- static String input() {
- String line;
- do {
- System.out.println("Input string that contains only ABC's:");
- line = scanner.nextLine();
- line.toLowerCase();
- } while(!checkInput(line));
- return line;
- }
- static String inputFile() {
- String line;
- boolean incorrect;
- do {
- incorrect = false;
- System.out.println("Input file's location:");
- line = scanner.nextLine();
- try (BufferedReader br = new BufferedReader(new FileReader(new File(line)))) {
- line = br.readLine();
- line.toLowerCase();
- incorrect = !checkInput(line);
- } catch (Exception e) {
- incorrect = true;
- System.out.println("Cannot open the file");
- }
- } while (incorrect);
- return line;
- }
- static boolean checkInput(String line) {
- for(char c : line.toCharArray()) {
- if(c != 'a' && c != 'b' && c != 'c') {
- System.out.println("String should contain only ABC's");
- return false;
- }
- }
- return true;
- }
- static String change(String line) {
- line = deletePattern(line, "aaaa", 0, 4, "");
- line = deletePattern(line, "baba", 0, 2, "");
- line = deletePattern(line, "abc", 0, 3, "");
- return line;
- }
- static String deletePattern(String line, String pattern, int i, int amount, String output) {
- String _output = output;
- if(i == line.length())
- return _output;
- if(line.charAt(i) == pattern.charAt(0) && isExact(line, pattern, i+1, 1))
- i += amount - 1;
- else
- _output += line.charAt(i);
- return deletePattern(line, pattern, i + 1, amount, _output);
- }
- static boolean isExact(String line, String pattern, int i1, int i2) {
- if(i1 == line.length() && i2 != pattern.length())
- return false;
- if(i2 == pattern.length())
- return true;
- if(line.charAt(i1) == pattern.charAt(i2))
- return isExact(line, pattern, i1+1, i2+1);
- return false;
- }
- static void saveToFile(String line) {
- System.out.println("Input location of a file:");
- String location;
- location = scanner.nextLine();
- try(BufferedWriter bw = new BufferedWriter(new FileWriter(new File(location), false))) {
- bw.append(line);
- System.out.println("File is saved");
- } catch (Exception e) {
- System.out.println("Cannot open the file");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement