Advertisement
EDGEUS

help(

Mar 10th, 2020
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         String choice;
  9.         choice=enterChoice();
  10.         if (choice.equals("Key"))
  11.             inputConsole();
  12.         else
  13.             fileReader();
  14.             System.out.println("temp");
  15.     }
  16.     static Scanner sc = new Scanner(System.in);
  17.     static String enterChoice() {
  18.         String choice;
  19.         boolean isCorrect;
  20.         System.out.println("Is the text entered from the keyboard or from a file?(Key or File)");
  21.         do {
  22.             choice = sc.nextLine();
  23.             if (choice.equals("Key") || choice.equals("File"))
  24.                 isCorrect = false;
  25.             else {
  26.                 System.out.println("Input error. Enter 'Key' or 'File'.");
  27.                 isCorrect = true;
  28.             }
  29.         }
  30.         while (isCorrect);
  31.         return choice;
  32.     }
  33.  
  34.     public static void writeResultFile(String buf){
  35.         System.out.println(solutionFile(buf));
  36.     }
  37.     public static void fileReader(){
  38.         try(FileReader reader = new FileReader("naw.txt"))
  39.         {
  40.             char[] buf = new char[256];
  41.             int c;
  42.             while((c = reader.read(buf))>0){
  43.  
  44.                 if(c < 256){
  45.                     buf = Arrays.copyOf(buf, c);
  46.                 }
  47.                 System.out.print(buf);
  48.                
  49.             }
  50.  
  51.         }
  52.         catch(IOException exo){
  53.  
  54.             System.out.println(exo.getMessage());
  55.         }
  56.  
  57.         writeResultFile(buf);
  58.  
  59.     }
  60.  
  61.     static HashSet<Character> solutionFile(String seq) {
  62.  
  63.         HashSet <Character> solSetFile = new HashSet<>();
  64.  
  65.         for (int i = 0; i < seq.length(); i++){
  66.             if (addHashFile().contains(seq.charAt(i))) {
  67.                 solSetFile.add(seq.charAt(i));
  68.             }
  69.         }
  70.         return solSetFile;
  71.     }
  72.  
  73.     static HashSet<Character> addHashFile(){
  74.         HashSet <Character> requiredChars = new HashSet<>();
  75.  
  76.         for (char i='0'; i<':';i++){
  77.             requiredChars.add(i);
  78.         }
  79.         requiredChars.add('(');
  80.         requiredChars.add(')');
  81.         requiredChars.add('[');
  82.         requiredChars.add(']');
  83.         requiredChars.add('{');
  84.         requiredChars.add('}');
  85.  
  86.         return requiredChars;
  87.  
  88.     }
  89.  
  90.     static void inputConsole(){
  91.         String example = null;
  92.         Scanner scan = new Scanner(System.in);
  93.         System.out.println("введите текст");
  94.         example = scan.next();
  95.         writeResult(example);
  96.     }
  97.  
  98.     public static void writeResult(String example){
  99.         System.out.println(solution(example));
  100.     }
  101.  
  102.  
  103.  
  104.     static HashSet<Character> solution(String seq) {
  105.  
  106.         HashSet <Character> solSet = new HashSet<>();
  107.  
  108.         for (int i = 0; i < seq.length(); i++){
  109.             if (addHash().contains(seq.charAt(i))) {
  110.                 solSet.add(seq.charAt(i));
  111.             }
  112.         }
  113.         return solSet;
  114.     }
  115.  
  116.     static HashSet<Character> addHash(){
  117.         HashSet <Character> requiredChars = new HashSet<>();
  118.  
  119.         for (char i='0'; i<':';i++){
  120.             requiredChars.add(i);
  121.         }
  122.         requiredChars.add('(');
  123.         requiredChars.add(')');
  124.         requiredChars.add('[');
  125.         requiredChars.add(']');
  126.         requiredChars.add('{');
  127.         requiredChars.add('}');
  128.  
  129.         return requiredChars;
  130.  
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement