Advertisement
Vladislav8653

laba_3_2_java

Dec 7th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6.  
  7. public class Lab2 {
  8.     private static final Scanner scan = new Scanner(System.in);
  9.     public static void main(String[] args) throws FileNotFoundException {
  10.         String sequence;
  11.         Set <Character> setOfSymbols;
  12.         System.out.println("Given a non-empty sequence of characters, it is required to construct and print a set whose elements are signs of arithmetic operations and digits that occur in the sequence.");
  13.         System.out.println("Type 1 - console input, type 0 - file input.");
  14.         boolean chose = choose();
  15.         if (chose)
  16.             sequence = inputSequence();
  17.         else {
  18.             String path = inputFilePath();
  19.             sequence = inputSequenceFromFile(path);
  20.         }
  21.         setOfSymbols = makeSet(sequence);
  22.         System.out.println("Type 1 - console output, type 0 - file output.");
  23.         chose = choose();
  24.         if (chose)
  25.             consoleOutput(setOfSymbols);
  26.         else {
  27.             fileOutput(setOfSymbols);
  28.         }
  29.     }
  30.     private static String inputSequence() {
  31.         String sequence;
  32.         System.out.println("Input Sequence");
  33.         sequence = scan.nextLine();
  34.         return sequence;
  35.     }
  36.  
  37.     private static String inputSequenceFromFile(String path) throws FileNotFoundException {
  38.         String sequence;
  39.         Scanner fileScanner;
  40.         fileScanner = new Scanner(new File(path));
  41.         sequence = fileScanner.nextLine();
  42.         return sequence;
  43.     }
  44.  
  45.     private static String inputFilePath() {
  46.         String path;
  47.         boolean isIncorrect;
  48.         do {
  49.             isIncorrect = false;
  50.             System.out.println("Input file path: ");
  51.             path = scan.nextLine();
  52.             File file = new File(path);
  53.             if (!path.endsWith(".txt")) {
  54.                 System.out.println("File Extension error");
  55.                 isIncorrect = true;
  56.             }
  57.             if (!isIncorrect && !file.exists()) {
  58.                 System.out.println("Wrong way to file. Input correct path.");
  59.                 isIncorrect = true;
  60.             }
  61.         } while(isIncorrect);
  62.         return path;
  63.     }
  64.  
  65.     private static boolean choose() {
  66.         int inputNumber = 0;
  67.         boolean isIncorrect;
  68.         final int MIN_NUM = 0;
  69.         final int MAX_NUM = 1;
  70.         do {
  71.             isIncorrect = false;
  72.             try {
  73.                 inputNumber = Integer.parseInt(scan.nextLine());
  74.             } catch (Exception e) {
  75.                 isIncorrect = true;
  76.                 System.out.println("Please, enter a integer number:");
  77.             }
  78.             if (inputNumber < MIN_NUM || inputNumber > MAX_NUM) {
  79.                 System.out.println("You are out of input range!:");
  80.                 isIncorrect = true;
  81.             }
  82.         } while (isIncorrect);
  83.         return inputNumber == 1;
  84.     }
  85.     private static Set<Character> makeSet(String sequence) {
  86.         HashSet<Character> setOfSymbols = new HashSet<>();
  87.         Set<Character> symbols = Set.of('/', '*', '-', '+', '%', '0', '1', '2', '3',
  88.                 '4', '5', '6', '7', '8', '9');
  89.         for (char i : symbols)
  90.         {
  91.             if (sequence.contains(String.valueOf(i)))
  92.                 setOfSymbols.add(i);
  93.         }
  94.         return setOfSymbols;
  95.     }
  96.  
  97.     private static void consoleOutput(Set<Character> setOfSymbols) {
  98.         for (Character setOfSymbol : setOfSymbols)
  99.             System.out.print(setOfSymbol + " ");
  100.     }
  101.  
  102.     private static void fileOutput(Set <Character> setOfSymbols) {
  103.         boolean isIncorrect;
  104.         Iterator <Character> output = setOfSymbols.iterator();
  105.         String path = inputFilePath();
  106.         do {
  107.             isIncorrect = false;
  108.             try {
  109.                 FileWriter writer = new FileWriter(path);
  110.  
  111.                 if (setOfSymbols.isEmpty())
  112.                     writer.write("You have not entered any signs of arithmetic operations or digits.");
  113.                 else {
  114.                     writer.write("The resulting set\n");
  115.                     while (output.hasNext()) {
  116.                         writer.write(output.next() + " ");
  117.                     }
  118.                 }
  119.                 writer.close();
  120.             } catch (IOException e) {
  121.                 isIncorrect = true;
  122.                 System.out.println("Mistake of output in file. Input path.");
  123.                 path = inputFilePath();
  124.             }
  125.         } while(isIncorrect);
  126.         System.out.println("Successful output in file.");
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement