Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.HashSet;
- import java.util.Scanner;
- public class lab3_2 {
- static Scanner scConsole = new Scanner(System.in);
- private static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- public static String getLineFromFile() {
- String str = null;
- Scanner file = getInputFileLocation();
- boolean isIncorrect = false;
- do {
- try {
- str = file.nextLine();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Enter natural number");
- }
- if ((!isIncorrect) && (str.length() == 0)) {
- System.out.println("The line is empty");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- private static String getLineFromConsole () {
- String str = null;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- str = scConsole.nextLine();
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if (str.length() == 0) {
- System.out.println("You entered an empty line!\nRepeat enter");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- private static String changeSentence(String str){
- HashSet<Character> LetSet = new HashSet<>();
- boolean isNotUnique;
- int i = 0;
- int temp = str.length();
- while (i < temp){
- isNotUnique = CheckUnique(str, i, LetSet);
- if (isNotUnique){
- StringBuilder sb = new StringBuilder(str);
- sb.delete(i,i+1);
- str = sb.toString();
- temp--;
- }else {
- LetSet.add(str.charAt(i));
- i++;
- }
- }
- return str;
- }
- private static boolean CheckUnique(String str, int i, HashSet LetSet){
- boolean isNotUnique;
- isNotUnique = LetSet.contains(str.charAt(i));
- return isNotUnique;
- }
- private static void outputToFile(String str) {
- PrintWriter out = getOutputFileLocation();
- out.print(str);
- out.close();
- }
- private static int chooseInput () {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- public static void main (String[]args){
- String str;
- System.out.println("Данная программа удаляет одинаковые элементы строки , оставляя один экземпляр символа.");
- int chosenInput = chooseInput();
- System.out.println("Enter the string");
- if (chosenInput == 0) {
- str = getLineFromFile();
- } else {
- str = getLineFromConsole();
- }
- str = changeSentence(str);
- System.out.println("Modified string:");
- System.out.println(str);
- outputToFile(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement