Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Iterator;
- import java.util.Scanner;
- import java.util.HashSet;
- import java.util.Set;
- public class Lab2 {
- private static final Scanner scan = new Scanner(System.in);
- public static void main(String[] args) throws FileNotFoundException {
- String sequence;
- Set <Character> setOfSymbols;
- System.out.println("Given a non-empty sequence of characters, it is required to construct and print a set whose elements are signs of arithmetic operations and digits that occur in the sequence.");
- System.out.println("Type 1 - console input, type 0 - file input.");
- boolean chose = choose();
- if (chose)
- sequence = inputSequence();
- else {
- String path = inputFilePath();
- sequence = inputSequenceFromFile(path);
- }
- setOfSymbols = makeSet(sequence);
- System.out.println("Type 1 - console output, type 0 - file output.");
- chose = choose();
- if (chose)
- consoleOutput(setOfSymbols);
- else {
- fileOutput(setOfSymbols);
- }
- }
- private static String inputSequence() {
- String sequence;
- System.out.println("Input Sequence");
- sequence = scan.nextLine();
- return sequence;
- }
- private static String inputSequenceFromFile(String path) throws FileNotFoundException {
- String sequence;
- Scanner fileScanner;
- fileScanner = new Scanner(new File(path));
- sequence = fileScanner.nextLine();
- return sequence;
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path: ");
- path = scan.nextLine();
- File file = new File(path);
- if (!path.endsWith(".txt")) {
- System.out.println("File Extension error");
- isIncorrect = true;
- }
- if (!isIncorrect && !file.exists()) {
- System.out.println("Wrong way to file. Input correct path.");
- isIncorrect = true;
- }
- } while(isIncorrect);
- return path;
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- if (inputNumber < MIN_NUM || inputNumber > MAX_NUM) {
- System.out.println("You are out of input range!:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputNumber == 1;
- }
- private static Set<Character> makeSet(String sequence) {
- HashSet<Character> setOfSymbols = new HashSet<>();
- Set<Character> symbols = Set.of('/', '*', '-', '+', '%', '0', '1', '2', '3',
- '4', '5', '6', '7', '8', '9');
- for (char i : symbols)
- {
- if (sequence.contains(String.valueOf(i)))
- setOfSymbols.add(i);
- }
- return setOfSymbols;
- }
- private static void consoleOutput(Set<Character> setOfSymbols) {
- for (Character setOfSymbol : setOfSymbols)
- System.out.print(setOfSymbol + " ");
- }
- private static void fileOutput(Set <Character> setOfSymbols) {
- boolean isIncorrect;
- Iterator <Character> output = setOfSymbols.iterator();
- String path = inputFilePath();
- do {
- isIncorrect = false;
- try {
- FileWriter writer = new FileWriter(path);
- if (setOfSymbols.isEmpty())
- writer.write("You have not entered any signs of arithmetic operations or digits.");
- else {
- writer.write("The resulting set\n");
- while (output.hasNext()) {
- writer.write(output.next() + " ");
- }
- }
- writer.close();
- } catch (IOException e) {
- isIncorrect = true;
- System.out.println("Mistake of output in file. Input path.");
- path = inputFilePath();
- }
- } while(isIncorrect);
- System.out.println("Successful output in file.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement