Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- import java.util.Scanner;
- import java.io.*;
- import java.util.HashSet;
- public class Laba3_2 {
- static Scanner scan = new Scanner(System.in);
- static char chooseIO(String text) {
- boolean isIncorrect;
- String choice;
- System.out.println("Выберите способ " + text + ": \nf - file\nc - console");
- do {
- isIncorrect = false;
- choice = scan.nextLine();
- if (choice.length() != 1) {
- System.err.println("Введите корректные данные!");
- isIncorrect = true;
- }
- if (!isIncorrect && !(choice.charAt(0) == 'f' || choice.charAt(0) == 'c')) {
- System.err.println("Введите корректные данные!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice.charAt(0);
- }
- static String takePath() {
- boolean isIncorrect;
- String path;
- System.out.println("Введите путь к файлу");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.err.println("Введите путь с расширением .txt");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- static String inputStringConsole() {
- System.out.println("Введите строку");
- String str = scan.nextLine();
- return str;
- }
- static String inputStringFile() {
- boolean isIncorrect;
- String str = "";
- String path;
- do {
- isIncorrect = false;
- path = takePath();
- try {
- Scanner fileReader = new Scanner(new File(path));
- str = fileReader.nextLine();
- if (fileReader.hasNext()) {
- System.out.println("Ошибка! В файле больше одной строки");
- isIncorrect = true;
- }
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("Ошибка доступа к файлу");
- } catch (NoSuchElementException e) {
- System.out.println("Ошибка!В файле не обнаружено предложения");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- static HashSet BuildSet(String str) {
- HashSet<Character> charsSet = new HashSet<>();
- for (int i = 0; i < str.length(); i++) {
- char symbol = str.charAt(i);
- if (!charsSet.contains(symbol)) {
- charsSet.add(symbol);
- }
- }
- return charsSet;
- }
- static String removeRedundantSymbols(String str, HashSet charsSet) {
- int length = str.length();
- String resStr = "";
- for (int i = 0; i < str.length(); i++) {
- if (charsSet.contains(str.charAt(i))) {
- resStr = resStr.concat(String.valueOf(str.charAt(i)));
- charsSet.remove(str.charAt(i));
- }
- }
- return resStr;
- }
- static void outputResStrConsole(String resStr) {
- System.out.println("Полученная строка:\n" + resStr);
- }
- static void outputResStrFile(String resStr) {
- boolean isIncorrect;
- String path;
- do {
- isIncorrect = false;
- path = takePath();
- try {
- FileWriter writer = new FileWriter(path);
- writer.write("Полученная строка:\n" + resStr);
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Ошибка записи в файл");
- }
- } while (isIncorrect);
- }
- public static void main(String[] args) {
- System.out.println("Программа удаляет из строки все вхождения символов кроме первых.");
- String str;
- char choice = chooseIO("ввода строки");
- if (choice == 'c')
- str = inputStringConsole();
- else
- str = inputStringFile();
- HashSet<Character> charsSet = BuildSet(str);
- String resStr = removeRedundantSymbols(str, charsSet);
- choice = chooseIO("вывода полученной строки");
- if (choice == 'c')
- outputResStrConsole(resStr);
- else
- outputResStrFile(resStr);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment