Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- static Scanner scanner;
- static String separators = " ,.:;-?!()[]{}@#$%&";
- static int getEndIndex(String inputString, int startIndex) {
- while (inputString.length()> startIndex &&!separators.contains(Character.toString(inputString.charAt(startIndex)))) {
- startIndex++;
- }
- startIndex--;
- return startIndex;
- }
- static int getNextWordIndex(String inputString, int index) {
- index++;
- while (inputString.length()> index &&separators.contains(Character.toString(inputString.charAt(index)))) {
- index++;
- }
- return index;
- }
- static String getWord(String inputString, int fromIndex, int toIndex) {
- String ret = "";
- if (toIndex > inputString.length() - 1) {
- toIndex = inputString.length() - 1;
- }
- toIndex++;
- for (int i = fromIndex; i < toIndex; i++) {
- ret += inputString.charAt(i);
- }
- return ret;
- }
- static String getInputType() {
- String ret = "";
- boolean isIncorrect = true;
- do {
- System.out.println("Выберите способ ввода предложения файл/консоль (ф/к)");
- ret = scanner.nextLine();
- if (ret.equalsIgnoreCase("файл") || ret.equalsIgnoreCase("ф")) {
- ret = "File";
- isIncorrect = false;
- } else if (ret.equalsIgnoreCase("консоль") || ret.equalsIgnoreCase("к")) {
- ret = "Console";
- isIncorrect = false;
- }
- } while (isIncorrect);
- return ret;
- }
- static String getInputFilePath() {
- String path;
- boolean isIncorrect = true;
- do {
- System.out.println("Введите абсолютный путь к файлу ");
- path = scanner.nextLine();
- File file = new File(path);
- if (file.isFile() && file.exists()) {
- isIncorrect = false;
- } else {
- System.out.println("Файл не найден");
- }
- } while (isIncorrect);
- return path;
- }
- static String getInputStringFromConsole() {
- String ret;
- System.out.println("Введите исходное предложение");
- ret = scanner.nextLine();
- return ret;
- }
- static String getInputStringFromFile() throws FileNotFoundException {
- String ret = "";
- String filePath;
- filePath = getInputFilePath();
- File file = new File(filePath);
- Scanner reader = new Scanner(file);
- ret = reader.nextLine();
- return ret;
- }
- static String getInputString() throws FileNotFoundException {
- String ret ="";
- String inputType;
- inputType = getInputType();
- if (inputType.equals("Console")) {
- ret = getInputStringFromConsole();
- } else if (inputType.equals("File")) {
- ret = getInputStringFromFile();
- }
- return ret;
- }
- static String swapTwoWords(String toSwap, int firstStartindex) {
- String word1, word2, separator;
- int firstEndIndex, secondStartIndex, secondEndIndex;
- firstEndIndex = getEndIndex(toSwap, firstStartindex);
- secondStartIndex = getNextWordIndex(toSwap, firstEndIndex);
- secondEndIndex = getEndIndex(toSwap, secondStartIndex);
- word1 = getWord(toSwap, firstStartindex, firstEndIndex);
- word2 = getWord(toSwap, secondStartIndex, secondEndIndex);
- separator = getWord(toSwap, firstEndIndex + 1, secondStartIndex - 1);
- return word2 + separator + word1;
- }
- static String swapString(String stringToSwap) {
- String ret ="";
- int i = 0;
- while (i < stringToSwap.length()) {
- if (separators.contains(Character.toString(stringToSwap.charAt(i))))
- {
- ret = ret + stringToSwap.charAt(i);
- i++;
- }
- else
- {
- ret = ret + swapTwoWords(stringToSwap, i);
- i = ret.length();
- }
- }
- return ret;
- }
- static String getOutputDirectory() {
- String ret;
- boolean isIncorrect = true;
- do {
- System.out.println("Введите директорию, в которую хотите сохранить вывод программы");
- ret = scanner.nextLine();
- File outputDirectory = new File(ret);
- if (outputDirectory.isDirectory() && outputDirectory.exists()) {
- isIncorrect = false;
- } else {
- System.out.println("Такой директории не существует.Попробуйте ещё раз");
- }
- } while (isIncorrect);
- return ret;
- }
- static void printOutputToFile(String input,String output) throws IOException
- {
- String directory;
- directory = getOutputDirectory();
- FileWriter myWriter = new FileWriter(directory + "\\output.txt");
- myWriter.write("Входная строка: \n");
- myWriter.write(input);
- myWriter.write("Выходная строка: \n");
- myWriter.write(output);
- myWriter.close();
- System.out.println("Файл сохранён по указанному пути");
- }
- static void printOutputToConsole(String input,String output)
- {
- System.out.println("Входная строка: \n"+input);
- System.out.println("Выходная строка: \n"+output);
- }
- public static void main(String[] args) throws IOException {
- scanner = new Scanner(System.in);
- String input,output="";
- input = getInputString();
- output = swapString(input);
- printOutputToConsole(input, output);
- printOutputToFile(input, output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement