Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.HashSet;
- public class Main {
- static boolean Choic() {
- Scanner scanner = new Scanner(System.in);
- boolean IsCorrect = true;
- String Choice;
- boolean Console = true;
- while(IsCorrect) {
- 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");
- Choice = scanner.nextLine();
- if ((Choice.equals("Y")) || (Choice.equals("y")) || (Choice.equals("N")) || (Choice.equals("n"))) {
- IsCorrect = false;
- if ((Choice.equals("Y")) || (Choice.equals("y"))) {
- Console = true;
- }
- else {
- Console = false;
- }
- }
- else {
- IsCorrect = true;
- System.out.println("This is mistake");
- }
- }
- return Console;
- }
- static StringBuffer EnterText() {
- Scanner scanner = new Scanner(System.in);
- 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.");
- StringBuffer Text = new StringBuffer(scanner.nextLine());
- return Text;
- }
- static StringBuffer EnterFileText() throws IOException {
- BufferedReader input = new BufferedReader(new FileReader("D:\\input.txt"));
- StringBuffer Text = new StringBuffer(input.readLine());
- return Text;
- }
- static StringBuffer EditText(boolean Choise) throws IOException {
- String[] StringArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "]", "}", "[", "{", "/", "-", "_", "=", "+", "@", "#", "$", "%", "^", "&", "*", "(", ")"};
- String[] StringArrays = {"!", ";", ":", ",", ".", "?"};
- HashSet<String> InvalidValues = new HashSet<String>(Arrays.asList(StringArray));
- HashSet<String> Values = new HashSet<String>(Arrays.asList(StringArrays));
- StringBuffer Text = new StringBuffer();
- if (Choise) {
- Text = EnterText();
- }
- else {
- Text = EnterFileText();
- }
- int Length = 0;
- int I = 1;
- Length = Text.length();
- while (I < Length) {
- if (InvalidValues.contains(Text.charAt(I))) {
- Text.delete(I,1);
- I--;
- Length--;
- }
- if ((Text.charAt(I) == ' ') && (Text.charAt(I + 1) == ' ')){
- Text.delete(I,1);
- I--;
- Length--;
- }
- if ((Values.contains(Text.charAt(I))) && (Values.contains(Text.charAt(I + 1)))) {
- Text.delete(I,1);
- I--;
- Length--;
- }
- I++;
- }
- return Text;
- }
- static void Encryption(boolean Choice) throws IOException {
- String[] StringArrays = {"!", ";", ":", ",", ".", "?"};
- HashSet<String> Values = new HashSet<String>(Arrays.asList(StringArrays));
- StringBuffer Text = new StringBuffer();
- Text = EditText(Choice);
- int Length = 0;
- int I = 1;
- Length = Text.length();
- String Temp;
- int Parity = 0;
- int Count = 0;
- while (I < Length) {
- Count++;
- if (Text.charAt(I) == ' ') {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substring(I - (Count - 1), I + 1);
- Temp = Temp.toUpperCase();
- Text.delete(I - (Count - 1), I + 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Text.insert(I - Count, "(");
- Text.insert(I + 1, ")");
- Length = Length + 2;
- I = I + 2;
- Count = 0;
- }
- if (Values.contains(Text.charAt(I + 1))) {
- I++;
- }
- }
- if ((Values.contains(Text.charAt(I))) && (I < Length) && (Text.charAt(I - 1) != ' ')) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substring(I - (Count - 1), I + 1);
- Temp = Temp.toUpperCase();
- Text.delete(I - (Count - 1), I + 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Text.insert(I - Count, "(");
- Text.insert(I + 1, ")");
- Length = Length + 2;
- I = I + 2;
- Count = 0;
- }
- if (Values.contains(Text.charAt(I + 1))) {
- I++;
- }
- }
- if ((I == Length - 1) && (Values.contains(Text.charAt(I)))) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substring(I - (Count - 1), I + 1);
- Temp = Temp.toUpperCase();
- Text.delete(I - (Count - 1), I + 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Length = Length + 2;
- Text.insert(I - (Count - 1), "(");
- Text.insert(I + 1, ")");
- I = I + 2;
- Count = 0;
- }
- }
- else {
- if (I == Length - 1) {
- Parity++;
- if (Parity % 2 == 0) {
- Temp = Text.substring(I - (Count - 1), I + 1);
- Temp = Temp.toUpperCase();
- Text.delete(I - (Count - 1), I + 1);
- Text.insert(I - (Count - 1), Temp);
- Count = 0;
- }
- else {
- Length = Length + 2;
- Text.insert(I - (Count - 1) , "(");
- Text.insert(I + 2, ")");
- I = I + 2;
- Count = 0;
- }
- }
- }
- I++;
- }
- if (Choice) {
- System.out.println(Text);
- } else {
- System.out.println(Text);
- File file = new File("D:\\output.txt");
- PrintWriter prwr = new PrintWriter(file);
- prwr.print(Text);
- prwr.close();
- }
- }
- static void body() throws IOException {
- boolean Choice = Choic();
- Encryption(Choice);
- }
- public static void main(String[] args) throws IOException {
- 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");
- body();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement