Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.String;
- import java.io.*;
- public class Main {
- private static final Scanner scanner = new Scanner(System.in);
- private static final String[] abc = new String[]{"аб", "бв", "вг", "гд", "де", "её", "ёж", "жз", "зи", "ий", "йк", "кл", "лм", "мн", "но", "оп", "пр", "рс", "ст", "ту", "уф", "фх", "хц", "цч", "чш", "шщ", "щъ", "ъы", "ыь", "ьэ", "эю", "юя"};
- public static void main(String[] args) {
- System.out.println("For pairs of adjacent letters (Cyrillic) occurring in a given text, indicate how many times each of these two-letter combinations occurs.");
- System.out.println("Enter type of input. " + '\n' + "1 is console input, 0 is file input.");
- boolean chose = choose();
- String strInput;
- String path;
- if (chose) {
- System.out.println("Input string without spaces:");
- strInput = consoleInputString();
- } else {
- path = inputFilePath();
- strInput = FileInputString(path);
- }
- int[] repeat = findRepetition(strInput);
- String[] combination = findPairs(strInput);
- System.out.println("Enter type of output. " + '\n' + "1 is console output, 0 is file output.");
- chose = choose();
- if (chose) {
- consoleOutput(repeat, combination);
- } else {
- path = inputFilePath();
- fileOutput(path, repeat, combination);
- }
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- //console
- private static String consoleInputString() {
- String strInput = "";
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- strInput = scanner.nextLine();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Check input data.");
- }
- } while (isIncorrect);
- return strInput;
- }
- private static int[] findRepetition(String strInput) {
- int counter = 0;
- final int SIZE = 32;
- int[] repeat = new int[SIZE];
- int i = 0;
- String strForTest;
- int whereIsPair;
- for (int j = 0; j < abc.length; j++) {
- strForTest = strInput;
- do {
- if (strForTest.contains(abc[j]) || strForTest.contains(abc[j].toUpperCase())) {
- counter++;
- whereIsPair = strForTest.indexOf(abc[j]);
- strForTest = strForTest.substring(whereIsPair + 1);
- }
- } while (strForTest.contains(abc[j]));
- if (counter != 0) {
- repeat[i] = counter;
- i++;
- }counter = 0;
- }
- return repeat;
- }
- private static String[] findPairs(String strInput) {
- int counter = 0;
- final int SIZE = 32;
- String[] combination = new String[SIZE];
- int i = 0;
- String strForTest;
- int whereIsPair;
- for (int j = 0; j < abc.length; j++) {
- strForTest = strInput;
- do {
- if (strForTest.contains(abc[j]) || strForTest.contains(abc[j].toUpperCase())) {
- counter++;
- whereIsPair = strForTest.indexOf(abc[j]);
- strForTest = strForTest.substring(whereIsPair + 1);
- }
- } while (strForTest.contains(abc[j]));
- if (counter != 0) {
- combination[i] = abc[j];
- i++;
- }
- counter = 0;
- }
- return combination;
- }
- private static int check (int[] repeat) {
- int counter = 0;
- final int SIZE = 32;
- for (int i = 0; i < repeat.length; i++) {
- if (repeat[i] == 0)
- counter++;
- }
- if (counter == SIZE)
- System.out.println("There are no pairs:(");
- return counter;
- }
- private static void consoleOutput(int[] repeat, String[] combination) {
- int counter = check(repeat);
- final int SIZE = 32;
- for (int i = 0; i < repeat.length; i++) {
- if (repeat[i] != 0 && counter != SIZE)
- System.out.println(combination[i] + " - " + repeat[i] + " times");
- }
- }
- //files
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static String FileInputString(String path) {
- String strInput = "";
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- Scanner fileScanner = new Scanner(new File(path));
- strInput = fileScanner.nextLine();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return strInput;
- }
- private static void fileOutput(String path, int[] repeat, String[] combination) {
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- for (int i = 0; i < repeat.length; i++) {
- if (repeat[i] != 0)
- writer.write(combination[i] + " - " + repeat[i] + " times");
- writer.write('\n');
- }
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Mistake of output in file. Input path.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- System.out.println("Successful output in file.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement