ryabov

Untitled

Nov 11th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. import java.util.NoSuchElementException;
  2. import java.util.Scanner;
  3. import java.io.*;
  4. import java.util.HashSet;
  5.  
  6. public class Laba3_2 {
  7.     static Scanner scan = new Scanner(System.in);
  8.  
  9.     static char chooseIO(String text) {
  10.         boolean isIncorrect;
  11.         String choice;
  12.         System.out.println("Выберите способ " + text + ": \nf - file\nc - console");
  13.         do {
  14.             isIncorrect = false;
  15.             choice = scan.nextLine();
  16.             if (choice.length() != 1) {
  17.                 System.err.println("Введите корректные данные!");
  18.                 isIncorrect = true;
  19.             }
  20.             if (!isIncorrect && !(choice.charAt(0) == 'f' || choice.charAt(0) == 'c')) {
  21.                 System.err.println("Введите корректные данные!");
  22.                 isIncorrect = true;
  23.             }
  24.         } while (isIncorrect);
  25.         return choice.charAt(0);
  26.     }
  27.  
  28.     static String takePath() {
  29.         boolean isIncorrect;
  30.         String path;
  31.         System.out.println("Введите путь к файлу");
  32.         do {
  33.             isIncorrect = false;
  34.             path = scan.nextLine();
  35.             if (!path.endsWith(".txt")) {
  36.                 System.err.println("Введите путь с расширением .txt");
  37.                 isIncorrect = true;
  38.             }
  39.         } while (isIncorrect);
  40.         return path;
  41.     }
  42.  
  43.     static String inputStringConsole() {
  44.         System.out.println("Введите строку");
  45.         String str = scan.nextLine();
  46.         return str;
  47.     }
  48.  
  49.     static String inputStringFile() {
  50.         boolean isIncorrect;
  51.         String str = "";
  52.         String path;
  53.         do {
  54.             isIncorrect = false;
  55.             path = takePath();
  56.             try {
  57.                 Scanner fileReader = new Scanner(new File(path));
  58.                 str = fileReader.nextLine();
  59.                 if (fileReader.hasNext()) {
  60.                     System.out.println("Ошибка! В файле больше одной строки");
  61.                     isIncorrect = true;
  62.                 }
  63.             } catch (FileNotFoundException e) {
  64.                 isIncorrect = true;
  65.                 System.out.println("Ошибка доступа к файлу");
  66.             } catch (NoSuchElementException e) {
  67.                 System.out.println("Ошибка!В файле не обнаружено предложения");
  68.                 isIncorrect = true;
  69.             }
  70.         } while (isIncorrect);
  71.         return str;
  72.     }
  73.  
  74.     static HashSet BuildSet(String str) {
  75.         HashSet<Character> charsSet = new HashSet<>();
  76.         for (int i = 0; i < str.length(); i++) {
  77.             char symbol = str.charAt(i);
  78.             if (!charsSet.contains(symbol)) {
  79.                 charsSet.add(symbol);
  80.             }
  81.         }
  82.         return charsSet;
  83.     }
  84.  
  85.     static String removeRedundantSymbols(String str, HashSet charsSet) {
  86.         int length = str.length();
  87.         String resStr = "";
  88.         for (int i = 0; i < str.length(); i++) {
  89.             if (charsSet.contains(str.charAt(i))) {
  90.                 resStr = resStr.concat(String.valueOf(str.charAt(i)));
  91.                 charsSet.remove(str.charAt(i));
  92.             }
  93.         }
  94.         return resStr;
  95.     }
  96.  
  97.     static void outputResStrConsole(String resStr) {
  98.         System.out.println("Полученная строка:\n" + resStr);
  99.     }
  100.  
  101.     static void outputResStrFile(String resStr) {
  102.         boolean isIncorrect;
  103.         String path;
  104.         do {
  105.             isIncorrect = false;
  106.             path = takePath();
  107.             try {
  108.                 FileWriter writer = new FileWriter(path);
  109.                 writer.write("Полученная строка:\n" + resStr);
  110.                 writer.close();
  111.             } catch (IOException e) {
  112.                 isIncorrect = true;
  113.                 System.out.println("Ошибка записи в файл");
  114.             }
  115.         } while (isIncorrect);
  116.     }
  117.  
  118.     public static void main(String[] args) {
  119.         System.out.println("Программа удаляет из строки все вхождения символов кроме первых.");
  120.         String str;
  121.         char choice = chooseIO("ввода строки");
  122.         if (choice == 'c')
  123.             str = inputStringConsole();
  124.         else
  125.             str = inputStringFile();
  126.         HashSet<Character> charsSet = BuildSet(str);
  127.         String resStr = removeRedundantSymbols(str, charsSet);
  128.         choice = chooseIO("вывода полученной строки");
  129.         if (choice == 'c')
  130.             outputResStrConsole(resStr);
  131.         else
  132.             outputResStrFile(resStr);
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment