Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- String choice;
- choice=enterChoice();
- if (choice.equals("Key"))
- inputConsole();
- else
- fileReader();
- System.out.println("temp");
- }
- static Scanner sc = new Scanner(System.in);
- static String enterChoice() {
- String choice;
- boolean isCorrect;
- System.out.println("Is the text entered from the keyboard or from a file?(Key or File)");
- do {
- choice = sc.nextLine();
- if (choice.equals("Key") || choice.equals("File"))
- isCorrect = false;
- else {
- System.out.println("Input error. Enter 'Key' or 'File'.");
- isCorrect = true;
- }
- }
- while (isCorrect);
- return choice;
- }
- public static void writeResultFile(String buf){
- System.out.println(solutionFile(buf));
- }
- public static void fileReader(){
- try(FileReader reader = new FileReader("naw.txt"))
- {
- char[] buf = new char[256];
- int c;
- while((c = reader.read(buf))>0){
- if(c < 256){
- buf = Arrays.copyOf(buf, c);
- }
- System.out.print(buf);
- }
- }
- catch(IOException exo){
- System.out.println(exo.getMessage());
- }
- writeResultFile(buf);
- }
- static HashSet<Character> solutionFile(String seq) {
- HashSet <Character> solSetFile = new HashSet<>();
- for (int i = 0; i < seq.length(); i++){
- if (addHashFile().contains(seq.charAt(i))) {
- solSetFile.add(seq.charAt(i));
- }
- }
- return solSetFile;
- }
- static HashSet<Character> addHashFile(){
- HashSet <Character> requiredChars = new HashSet<>();
- for (char i='0'; i<':';i++){
- requiredChars.add(i);
- }
- requiredChars.add('(');
- requiredChars.add(')');
- requiredChars.add('[');
- requiredChars.add(']');
- requiredChars.add('{');
- requiredChars.add('}');
- return requiredChars;
- }
- static void inputConsole(){
- String example = null;
- Scanner scan = new Scanner(System.in);
- System.out.println("введите текст");
- example = scan.next();
- writeResult(example);
- }
- public static void writeResult(String example){
- System.out.println(solution(example));
- }
- static HashSet<Character> solution(String seq) {
- HashSet <Character> solSet = new HashSet<>();
- for (int i = 0; i < seq.length(); i++){
- if (addHash().contains(seq.charAt(i))) {
- solSet.add(seq.charAt(i));
- }
- }
- return solSet;
- }
- static HashSet<Character> addHash(){
- HashSet <Character> requiredChars = new HashSet<>();
- for (char i='0'; i<':';i++){
- requiredChars.add(i);
- }
- requiredChars.add('(');
- requiredChars.add(')');
- requiredChars.add('[');
- requiredChars.add(']');
- requiredChars.add('{');
- requiredChars.add('}');
- return requiredChars;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement