Advertisement
Vanya_Shestakov

laba3.2 (Java)

Oct 22nd, 2020 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. package com.company;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.lang.*;
  7. import java.util.Arrays;
  8. import java.util.HashSet;
  9. import java.util.Scanner;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         System.out.println("The program describes the set of vowels and consonants of the Russian language,\n" +
  15.         " and determines the number of vowels and consonants in a sentence entered from the keyboard.");
  16.         int source = chooseSource();
  17.         String line = inputData(source);
  18.         int numberOfVowels = calculateVowels(line);
  19.         int numberOfConsonants = calculateConsonants(line);
  20.         outputData(source, numberOfVowels, numberOfConsonants);
  21.     }
  22.  
  23.     public static int chooseSource() {
  24.         System.out.println("Choose where to enter data. Enter 1 or 2:\n 1.File\n 2.Console");
  25.         Scanner scan = new Scanner(System.in);
  26.         int choice = 0;
  27.         boolean isIncorrect;
  28.         do {
  29.             isIncorrect = false;
  30.             try {
  31.                 choice = Integer.parseInt(scan.nextLine());
  32.             }catch (Exception e){
  33.                 System.out.println("Enter an integer!");
  34.                 isIncorrect = true;
  35.             }
  36.             if (!isIncorrect && choice != 1 && choice != 2){
  37.                 System.out.println("Enter 1 or 2!");
  38.                 isIncorrect = true;
  39.             }
  40.         }while (isIncorrect);
  41.         return choice;
  42.     }
  43.  
  44.     public static String inputData(int source) throws FileNotFoundException {
  45.         String line = "";
  46.         switch (source){
  47.             case 1:
  48.                 System.out.println("Enter the absolute link to the input file");
  49.                 String pathInput = inputPath();
  50.                 line = inputLineFromFile(pathInput);
  51.                 break;
  52.             case 2:
  53.                 line = inputLineFromConsole();
  54.                 break;
  55.         }
  56.         return line;
  57.     }
  58.  
  59.     public static String inputPath() {
  60.         Scanner scan = new Scanner(System.in);
  61.         boolean isIncorrect;
  62.         String path;
  63.         do {
  64.             isIncorrect = false;
  65.             path = scan.nextLine();
  66.             File file = new File(path);
  67.  
  68.             if (!file.exists()){
  69.                 System.out.println("File not found!\nEnter the absolute link to the file");
  70.                 isIncorrect = true;
  71.             }
  72.         }while (isIncorrect);
  73.         System.out.println();
  74.         return path;
  75.     }
  76.  
  77.     public static String inputLineFromFile(String path) throws FileNotFoundException {
  78.         File inputFile = new File(path);
  79.         Scanner scan = new Scanner(inputFile);
  80.         String line;
  81.         if (inputFile.length() != 0){
  82.             line = scan.nextLine();
  83.         }else{
  84.             System.out.println("The line is missing from your file!\nEnter it from console ");
  85.             line = inputLineFromConsole();
  86.         }
  87.         return line;
  88.     }
  89.  
  90.     public static String inputLineFromConsole() {
  91.         Scanner scan = new Scanner(System.in);
  92.         boolean isIncorrect;
  93.         String line;
  94.         System.out.println("Enter the line");
  95.         do {
  96.             isIncorrect = false;
  97.             line = scan.nextLine();
  98.             if (line.length() == 0){
  99.                 System.out.println("You entered an empty line!\nRepeat enter");
  100.                 isIncorrect = true;
  101.             }
  102.         }while (isIncorrect);
  103.         return line;
  104.     }
  105.  
  106.     public static int calculateVowels(String line){
  107.         HashSet<Character> vowelsLetters = new HashSet<>(Arrays.asList('а','о','у','ы','э','я','ё','ю','и','е'));
  108.         int amount = 0;
  109.         for (int i = 0; i < line.length(); i++){
  110.             if (vowelsLetters.contains(Character.toLowerCase(line.charAt(i)))) {
  111.                 amount++;
  112.             }
  113.         }
  114.         return amount;
  115.     }
  116.  
  117.     public static int calculateConsonants(String line){
  118.         HashSet<Character> consonantsLetters = new HashSet<>(Arrays.asList('б','в','г','д','ж','з','й','к','л','м','н','п','р','с','т','ф','х','ц','ч','ш','щ'));
  119.         int amount = 0;
  120.         for (int i = 0; i < line.length(); i++){
  121.             if (consonantsLetters.contains(Character.toLowerCase(line.charAt(i)))) {
  122.                 amount++;
  123.             }
  124.         }
  125.         return amount;
  126.     }
  127.  
  128.     public static void outputData(int source, int numberOfVowels, int numberOfConsonants ) throws IOException {
  129.         if (source == 1) {
  130.             System.out.println("\nEnter the absolute link to the output file");
  131.             String pathOutput = inputPath();
  132.             outputToFile(pathOutput, numberOfVowels, numberOfConsonants);
  133.         }else {
  134.             outputToConsole(numberOfVowels, numberOfConsonants);
  135.         }
  136.     }
  137.  
  138.     public static void outputToFile(String path, int numberOfVowels, int numberOfConsonants) throws IOException {
  139.         FileWriter writer = new FileWriter(path);
  140.         writer.write("Vowels: " + numberOfVowels + "\nConsonants: " + numberOfConsonants);
  141.         writer.close();
  142.         System.out.println("The data is successfully recorded in the file");
  143.     }
  144.  
  145.     public static void outputToConsole(int numberOfVowels, int numberOfConsonants){
  146.         System.out.println("\nVowels: " + numberOfVowels + "\nConsonants: " + numberOfConsonants);
  147.     }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement