Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.LinkedHashSet;
  3. import java.util.Scanner;
  4.  
  5. public class name2 {
  6. public static void main(String[] args) {
  7. chooseInputMethod();
  8. String input = chooseMethod();
  9. String resolve = chooseWayToResolve(input);
  10. createOutput(resolve);
  11. }
  12.  
  13. private static String chooseWayToResolve(String input) {
  14. String resolve;
  15. if (input.equals("1")) {
  16. readConsole();
  17. String process = scan();
  18. LinkedHashSet<String> set = createSet(process);
  19. resolve = set.toString();
  20. } else {
  21. readPath();
  22. String process = scan();
  23. process = readFile(process);
  24. LinkedHashSet<String> set = createSet(process);
  25. resolve = set.toString();
  26. }
  27. return resolve;
  28. }
  29.  
  30. private static String scan() {
  31. Scanner scanner = new Scanner(System.in);
  32. return scanner.nextLine();
  33. }
  34.  
  35. private static void chooseInputMethod() {
  36. System.out.println("Chose input method");
  37. System.out.println("Console - write 1");
  38. System.out.println("File - write 0");
  39. }
  40.  
  41. private static String chooseMethod() {
  42. Scanner scan = new Scanner(System.in);
  43. boolean isNotCorrect;
  44. String choice;
  45. do {
  46. isNotCorrect = false;
  47. choice = scan.nextLine();
  48. if ((!choice.equals("0")) && (!choice.equals("1"))) {
  49. System.out.println("Incorrect input method. Try again");
  50. isNotCorrect = true;
  51. }
  52. } while (isNotCorrect);
  53. return choice;
  54. }
  55.  
  56. private static void readConsole() {
  57. System.out.println("Enter your text: ");
  58. }
  59.  
  60. private static void readPath() {
  61. System.out.println("Enter file path: ");
  62. }
  63.  
  64. private static void readNewPath() {
  65. System.out.println("Incorrect Path! Try again: ");
  66. }
  67.  
  68. private static String readFile(String path) {
  69. String data = "";
  70. File file = new File(path);
  71. try {
  72. Scanner scanner = new Scanner(file);
  73. while (scanner.hasNextLine()) {
  74. data = scanner.nextLine();
  75. }
  76. scanner.close();
  77. } catch (FileNotFoundException e) {
  78. readNewPath();
  79. String newData = scan();
  80. data = readFile(newData);
  81. }
  82. return data;
  83. }
  84.  
  85. private static void createOutput(String read) {
  86. System.out.println(read);
  87. }
  88.  
  89. private static LinkedHashSet<String> createSet(String line) {
  90. LinkedHashSet<String> set = new LinkedHashSet<>();
  91. for (int i = 0; i < line.length(); i++) {
  92. if (line.charAt(i) > (char) 41 && line.charAt(i) < (char) 58) {
  93. set.add(line.charAt(i) + " ");
  94. }
  95. }
  96. return set;
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement