Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.LinkedHashSet;
- import java.util.Scanner;
- public class name2 {
- public static void main(String[] args) {
- chooseInputMethod();
- String input = chooseMethod();
- String resolve = chooseWayToResolve(input);
- createOutput(resolve);
- }
- private static String chooseWayToResolve(String input) {
- String resolve;
- if (input.equals("1")) {
- readConsole();
- String process = scan();
- LinkedHashSet<String> set = createSet(process);
- resolve = set.toString();
- } else {
- readPath();
- String process = scan();
- process = readFile(process);
- LinkedHashSet<String> set = createSet(process);
- resolve = set.toString();
- }
- return resolve;
- }
- private static String scan() {
- Scanner scanner = new Scanner(System.in);
- return scanner.nextLine();
- }
- private static void chooseInputMethod() {
- System.out.println("Chose input method");
- System.out.println("Console - write 1");
- System.out.println("File - write 0");
- }
- private static String chooseMethod() {
- Scanner scan = new Scanner(System.in);
- boolean isNotCorrect;
- String choice;
- do {
- isNotCorrect = false;
- choice = scan.nextLine();
- if ((!choice.equals("0")) && (!choice.equals("1"))) {
- System.out.println("Incorrect input method. Try again");
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- return choice;
- }
- private static void readConsole() {
- System.out.println("Enter your text: ");
- }
- private static void readPath() {
- System.out.println("Enter file path: ");
- }
- private static void readNewPath() {
- System.out.println("Incorrect Path! Try again: ");
- }
- private static String readFile(String path) {
- String data = "";
- File file = new File(path);
- try {
- Scanner scanner = new Scanner(file);
- while (scanner.hasNextLine()) {
- data = scanner.nextLine();
- }
- scanner.close();
- } catch (FileNotFoundException e) {
- readNewPath();
- String newData = scan();
- data = readFile(newData);
- }
- return data;
- }
- private static void createOutput(String read) {
- System.out.println(read);
- }
- private static LinkedHashSet<String> createSet(String line) {
- LinkedHashSet<String> set = new LinkedHashSet<>();
- for (int i = 0; i < line.length(); i++) {
- if (line.charAt(i) > (char) 41 && line.charAt(i) < (char) 58) {
- set.add(line.charAt(i) + " ");
- }
- }
- return set;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement