Advertisement
grioool

Untitled

Nov 10th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package OAiP;
  2.  
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class Program2 {
  10.  
  11. public static void main(String[] args) throws IOException {
  12. System.out.println("This program encrypt string according to the encoding table.\n");
  13. Scanner in = new Scanner(System.in);
  14. String reader[];
  15. String text = "", st1 = "", st2 = "";
  16. boolean inValid = true;
  17. do {
  18. System.out.println("Enter '1' if you want to load data from console, and '2' from file: ");
  19. switch (in.nextLine()) {
  20. case "1":
  21. reader = loadData();
  22. text = reader[0];
  23. st1 = reader[1];
  24. st2 = reader[2];
  25. inValid = false;
  26. break;
  27. case "2":
  28. reader = loadDataFromFile();
  29. text = reader[0];
  30. st1 = reader[1];
  31. st2 = reader[2];
  32. inValid = false;
  33. break;
  34. }
  35. } while (inValid);
  36. outputData(encrypter(text, st1, st2));
  37. }
  38.  
  39. public static String[] loadDataFromFile() throws IOException {
  40. Scanner in = new Scanner(System.in);
  41. FileReader inF = new FileReader("orginal_string.txt");
  42. Scanner fileScanner = new Scanner(inF);
  43. boolean inValid = true;
  44. do {
  45. try {
  46. System.out.println("Enter input file directory: ");
  47. inF = new FileReader(in.nextLine());
  48. inValid = false;
  49. } catch (Exception e) {
  50. System.out.println("File not found");
  51. }
  52. } while (inValid);
  53. String reader[] = new String[3];
  54. reader[0] = fileScanner.nextLine();
  55. reader[1] = fileScanner.nextLine();
  56. reader[2] = fileScanner.nextLine();
  57. inF.close();
  58. return reader;
  59. }
  60.  
  61. public static String encrypter(String text, String st1, String st2) {
  62. for (int i = 0; i < st1.length(); i++) {
  63. text = text.replaceAll(st1.charAt(i) + "", st2.charAt(i) + "");
  64. }
  65. return text;
  66. }
  67.  
  68. public static void outputData(String text) throws IOException {
  69. Scanner in = new Scanner(System.in);
  70. System.out.println("Enter output file directory: ");
  71. FileWriter outF = new FileWriter(in.nextLine());
  72. System.out.println("Encrypted string: " + text);
  73. outF.write("Encrypted string: " + text);
  74. outF.close();
  75. }
  76.  
  77. public static String[] loadData() {
  78. Scanner in = new Scanner(System.in);
  79. String reader[] = new String[3];
  80. System.out.println("Enter string for encrypting: ");
  81. reader[0] = in.nextLine();
  82. boolean inValid = true;
  83. do {
  84. System.out.println("Enter original string according to encoding table: ");
  85. reader[1] = in.nextLine();
  86. System.out.println("Enter final string according to encoding table: ");
  87. reader[2] = in.nextLine();
  88. if (reader[1].length() == reader[2].length()) {
  89. inValid = false;
  90. } else {
  91. System.out.println("Input error. Strings must have the same length.");
  92. }
  93. } while (inValid);
  94. return reader;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement