Advertisement
venik2405

lab3_2_0

Nov 30th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.HashSet;
  3. import java.util.Scanner;
  4.  
  5. public class lab3_2 {
  6.  
  7.     static Scanner scConsole = new Scanner(System.in);
  8.  
  9.     private static PrintWriter getOutputFileLocation() {
  10.         boolean isIncorrect;
  11.         String location;
  12.         PrintWriter file = null;
  13.         do {
  14.             isIncorrect = false;
  15.             System.out.println("Enter file location:");
  16.             location = scConsole.nextLine();
  17.             try {
  18.                 file = new PrintWriter(location);
  19.             } catch (FileNotFoundException e) {
  20.                 isIncorrect = true;
  21.                 System.out.println("File with this location is not found");
  22.             }
  23.         } while (isIncorrect);
  24.         if (file != null)
  25.             System.out.println("File opened successfully");
  26.         return file;
  27.     }
  28.  
  29.     private static Scanner getInputFileLocation() {
  30.         boolean isIncorrect;
  31.         String location;
  32.         Scanner file = null;
  33.         do {
  34.             isIncorrect = false;
  35.             System.out.println("Enter file location:");
  36.             location = scConsole.nextLine();
  37.             try {
  38.                 file = new Scanner(new File(location));
  39.             } catch (FileNotFoundException e) {
  40.                 isIncorrect = true;
  41.                 System.out.println("File with this location is not found");
  42.             }
  43.         } while (isIncorrect);
  44.         if (file != null)
  45.             System.out.println("File opened successfully");
  46.         return file;
  47.     }
  48.  
  49.     public static String getLineFromFile() {
  50.         String str = null;
  51.         Scanner file = getInputFileLocation();
  52.         boolean isIncorrect = false;
  53.         do {
  54.             try {
  55.                 str = file.nextLine();
  56.             } catch (Exception e) {
  57.                 isIncorrect = true;
  58.                 System.out.println("Enter natural number");
  59.             }
  60.             if ((!isIncorrect) && (str.length() == 0)) {
  61.                 System.out.println("The line is empty");
  62.                 isIncorrect = true;
  63.             }
  64.         } while (isIncorrect);
  65.         return str;
  66.     }
  67.  
  68.  
  69.     private static String getLineFromConsole () {
  70.         String str = null;
  71.         boolean isIncorrect;
  72.         do {
  73.             isIncorrect = false;
  74.             try {
  75.                 str = scConsole.nextLine();
  76.             } catch (Exception e) {
  77.                 System.out.println("Enter the number");
  78.                 isIncorrect = true;
  79.             }
  80.             if (str.length() == 0) {
  81.                 System.out.println("You entered an empty line!\nRepeat enter");
  82.                 isIncorrect = true;
  83.             }
  84.         } while (isIncorrect);
  85.         return str;
  86.     }
  87.  
  88.  
  89.     private static String changeSentence(String str){
  90.         HashSet<Character> LetSet = new HashSet<>();
  91.         boolean isNotUnique;
  92.         int i = 0;
  93.         int temp = str.length();
  94.         while (i < temp){
  95.             isNotUnique = CheckUnique(str, i, LetSet);
  96.             if (isNotUnique){
  97.                 StringBuilder sb = new StringBuilder(str);
  98.                 sb.delete(i,i+1);
  99.                 str = sb.toString();
  100.                 temp--;
  101.             }else {
  102.                 LetSet.add(str.charAt(i));
  103.                 i++;
  104.             }
  105.         }
  106.         return str;
  107.     }
  108.  
  109.     private static boolean CheckUnique(String str, int i, HashSet LetSet){
  110.         boolean isNotUnique;
  111.         isNotUnique = LetSet.contains(str.charAt(i));
  112.         return isNotUnique;
  113.     }
  114.  
  115.  
  116.     private static void outputToFile(String str) {
  117.         PrintWriter out = getOutputFileLocation();
  118.         out.print(str);
  119.         out.close();
  120.     }
  121.  
  122.     private static int chooseInput () {
  123.         boolean isIncorrect;
  124.         String line;
  125.         do {
  126.             isIncorrect = false;
  127.             System.out.println("Do you want to input from file? (y/n)");
  128.             line = scConsole.nextLine().toLowerCase();
  129.             if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
  130.                 isIncorrect = true;
  131.                 System.out.println("Enter valid answer");
  132.             }
  133.         } while (isIncorrect);
  134.         if (line.equals("y") || line.equals("")) {
  135.             return 0;
  136.         } else {
  137.             return 1;
  138.         }
  139.     }
  140.  
  141.     public static void main (String[]args){
  142.         String str;
  143.         System.out.println("Данная программа удаляет одинаковые элементы строки , оставляя один экземпляр символа.");
  144.         int chosenInput = chooseInput();
  145.         System.out.println("Enter the string");
  146.         if (chosenInput == 0) {
  147.             str = getLineFromFile();
  148.         } else {
  149.             str = getLineFromConsole();
  150.         }
  151.         str = changeSentence(str);
  152.         System.out.println("Modified string:");
  153.         System.out.println(str);
  154.         outputToFile(str);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement