Advertisement
nikitaxe132

lab3.1

Nov 20th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.09 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6. import java.util.HashSet;
  7.  
  8. public class Main {
  9.     static boolean Choic() {
  10.         Scanner scanner = new Scanner(System.in);
  11.         boolean IsCorrect = true;
  12.         String Choice;
  13.         boolean Console = true;
  14.         while(IsCorrect) {
  15.             System.out.println("If you want to enter data from the console, enter Y or y, and if from the file, enter N or n");
  16.             Choice = scanner.nextLine();
  17.             if ((Choice.equals("Y")) || (Choice.equals("y")) || (Choice.equals("N")) || (Choice.equals("n"))) {
  18.                 IsCorrect = false;
  19.                 if ((Choice.equals("Y")) || (Choice.equals("y"))) {
  20.                     Console = true;
  21.                 }
  22.                 else {
  23.                     Console = false;
  24.                 }
  25.             }
  26.             else {
  27.                 IsCorrect = true;
  28.                 System.out.println("This is mistake");
  29.             }
  30.         }
  31.         return Console;
  32.     }
  33.  
  34.     static StringBuffer EnterText() {
  35.         Scanner scanner = new Scanner(System.in);
  36.         System.out.println("Enter the text. All special characters and numbers such as 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, /, -, _, =, +, @, #, $, %, ^, &, *, (, ) will be deleted.");
  37.         StringBuffer Text = new StringBuffer(scanner.nextLine());
  38.         return Text;
  39.     }
  40.  
  41.     static StringBuffer EnterFileText() throws IOException {
  42.         BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
  43.         StringBuffer Text = new StringBuffer(input.readLine());
  44.         return Text;
  45.     }
  46.  
  47.     static StringBuffer EditText(boolean Choise) throws IOException {
  48.         String[] StringArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "]", "}", "[", "{", "/", "-", "_", "=", "+", "@", "#", "$", "%", "^", "&", "*", "(", ")"};
  49.         String[] StringArrays = {"!", ";", ":", ",", ".", "?"};
  50.         HashSet<String> InvalidValues = new HashSet<String>(Arrays.asList(StringArray));
  51.         HashSet<String> Values = new HashSet<String>(Arrays.asList(StringArrays));
  52.         StringBuffer Text = new StringBuffer();
  53.         if (Choise) {
  54.             Text = EnterText();
  55.         }
  56.         else {
  57.             Text = EnterFileText();
  58.         }
  59.         int Length = 0;
  60.         int I = 1;
  61.         Length = Text.length();
  62.         while (I < Length) {
  63.             if (InvalidValues.contains(Text.charAt(I))) {
  64.                 Text.delete(I,1);
  65.                 I--;
  66.                 Length--;
  67.             }
  68.             if ((Text.charAt(I) == ' ') && (Text.charAt(I + 1) == ' ')){
  69.                 Text.delete(I,1);
  70.                 I--;
  71.                 Length--;
  72.             }
  73.             if ((Values.contains(Text.charAt(I))) && (Values.contains(Text.charAt(I + 1)))) {
  74.                 Text.delete(I,1);
  75.                 I--;
  76.                 Length--;
  77.             }
  78.             I++;
  79.         }
  80.         return Text;
  81.     }
  82.  
  83.     static void Encryption(boolean Choice) throws IOException {
  84.         String[] StringArrays = {"!", ";", ":", ",", ".", "?"};
  85.         HashSet<String> Values = new HashSet<String>(Arrays.asList(StringArrays));
  86.         StringBuffer Text = new StringBuffer();
  87.         Text = EditText(Choice);
  88.         int Length = 0;
  89.         int I = 1;
  90.         Length = Text.length();
  91.         String Temp;
  92.         int Parity = 0;
  93.         int Count = 0;
  94.         while (I < Length) {
  95.             Count++;
  96.             if (Text.charAt(I) == ' ') {
  97.                 Parity++;
  98.                 if (Parity % 2 == 0) {
  99.                     Temp = Text.substring(I - (Count - 1), I + 1);
  100.                     Temp = Temp.toUpperCase();
  101.                     Text.delete(I - (Count - 1), I + 1);
  102.                     Text.insert(I - (Count - 1), Temp);
  103.                     Count = 0;
  104.                 }
  105.                 else {
  106.                     Text.insert(I - Count, "(");
  107.                     Text.insert(I + 1, ")");
  108.                     Length = Length + 2;
  109.                     I = I + 2;
  110.                     Count = 0;
  111.                 }
  112.                 if (Values.contains(Text.charAt(I + 1))) {
  113.                     I++;
  114.                 }
  115.             }
  116.             if ((Values.contains(Text.charAt(I))) && (I < Length) && (Text.charAt(I - 1) != ' ')) {
  117.                 Parity++;
  118.                 if (Parity % 2 == 0) {
  119.                     Temp = Text.substring(I - (Count - 1), I + 1);
  120.                     Temp = Temp.toUpperCase();
  121.                     Text.delete(I - (Count - 1), I + 1);
  122.                     Text.insert(I - (Count - 1), Temp);
  123.                     Count = 0;
  124.                 }
  125.                 else {
  126.                     Text.insert(I - Count, "(");
  127.                     Text.insert(I + 1, ")");
  128.                     Length = Length + 2;
  129.                     I = I + 2;
  130.                     Count = 0;
  131.                 }
  132.                 if (Values.contains(Text.charAt(I + 1))) {
  133.                     I++;
  134.                 }
  135.             }
  136.             if ((I == Length - 1) && (Values.contains(Text.charAt(I)))) {
  137.                 Parity++;
  138.                 if (Parity % 2 == 0) {
  139.                     Temp = Text.substring(I - (Count - 1), I + 1);
  140.                     Temp = Temp.toUpperCase();
  141.                     Text.delete(I - (Count - 1), I + 1);
  142.                     Text.insert(I - (Count - 1), Temp);
  143.                     Count = 0;
  144.                 }
  145.                 else {
  146.                     Length = Length + 2;
  147.                     Text.insert(I - (Count - 1), "(");
  148.                     Text.insert(I + 1, ")");
  149.                     I = I + 2;
  150.                     Count = 0;
  151.                 }
  152.             }
  153.             else {
  154.                 if (I == Length - 1) {
  155.                     Parity++;
  156.                     if (Parity % 2 == 0) {
  157.                         Temp = Text.substring(I - (Count - 1), I + 1);
  158.                         Temp = Temp.toUpperCase();
  159.                         Text.delete(I - (Count - 1), I + 1);
  160.                         Text.insert(I - (Count - 1), Temp);
  161.                         Count = 0;
  162.                     }
  163.                     else {
  164.                         Length = Length + 2;
  165.                         Text.insert(I - (Count - 1) , "(");
  166.                         Text.insert(I + 2, ")");
  167.                         I = I + 2;
  168.                         Count = 0;
  169.                     }
  170.                 }
  171.             }
  172.             I++;
  173.         }
  174.         if (Choice) {
  175.             System.out.println(Text);
  176.         } else {
  177.             System.out.println(Text);
  178.             File file = new File("D:\\output.txt");
  179.             PrintWriter prwr = new PrintWriter(file);
  180.             prwr.print(Text);
  181.             prwr.close();
  182.         }
  183.     }
  184.  
  185.     static void body() throws IOException {
  186.         boolean Choice = Choic();
  187.         Encryption(Choice);
  188.     }
  189.     public static void main(String[] args) throws IOException {
  190.        System.out.println("This program in the text in every even word replaces all lowercase alphabetic characters with uppercase, and each odd word is enclosed in parentheses");
  191.        body();
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement