Egor_Vakar

lab3_1(Java)

Nov 2nd, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.38 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.util.Scanner;
  6.  
  7. public class lab3_1 {
  8.     static Scanner scan = new Scanner(System.in);
  9.  
  10.     public static void main(String[] args){
  11.         System.out.print("Welcome to the program that count how many times the last word occurs in a sentence.\nSelect the source for entering the sentence:\n1:Console\n2:File\nEnter 1 or 2: ");
  12.         byte inputSource = takeSource();
  13.         String sentence = takeSentence(inputSource);
  14.         String[] arrayOfWords = findArrayOfWords(sentence);
  15.         int answer = findAnswer(arrayOfWords);
  16.         System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
  17.         byte outputSource = takeSource();
  18.         output(outputSource, sentence, answer);
  19.         scan.close();
  20.     }
  21.  
  22.     public static byte takeSource(){
  23.         boolean isIncorrect;
  24.         byte choice = 0;
  25.         final byte CONSOLE = 1;
  26.         final byte FILE = 2;
  27.         do {
  28.             isIncorrect = false;
  29.             try {
  30.                 choice = Byte.parseByte(scan.nextLine());
  31.             } catch (Exception e) {
  32.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  33.                 isIncorrect = true;
  34.             }
  35.             if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
  36.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  37.                 isIncorrect = true;
  38.             }
  39.         } while (isIncorrect);
  40.         return choice;
  41.     }
  42.  
  43.     public static String takeInPath(){
  44.         String path;
  45.         boolean isIncorrect;
  46.         System.out.print("Enter file path: ");
  47.         do {
  48.             isIncorrect = false;
  49.             path = scan.nextLine();
  50.             if (Files.notExists(Path.of(path))) {
  51.                 System.out.print("File is not found\nEnter file path: ");
  52.                 isIncorrect = true;
  53.             }
  54.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  55.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  56.                 isIncorrect = true;
  57.             }
  58.         }while (isIncorrect);
  59.         return path;
  60.     }
  61.  
  62.     public static String takeOutPath(){
  63.         String path;
  64.         boolean isIncorrect;
  65.         System.out.print("Enter file path: ");
  66.         do {
  67.             isIncorrect = false;
  68.             path = scan.nextLine();
  69.             if (!path.endsWith(".txt")) {
  70.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  71.                 isIncorrect = true;
  72.             }
  73.         }while (isIncorrect);
  74.         return path;
  75.     }
  76.  
  77.     public static String takeSentenceFromFile(final String path){
  78.         String sentence = "";
  79.         boolean isCorrect = true;
  80.         try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
  81.             sentence = reader.readLine();
  82.             if ((reader.readLine()) != null){
  83.                 isCorrect = false;
  84.                 System.out.println("Incorrect file data!!");
  85.             }
  86.         }catch (IOException e) {
  87.             System.out.println("Input/Output error!!!");
  88.             isCorrect = false;
  89.         }
  90.         if(isCorrect) {
  91.             return sentence;
  92.         }else{
  93.             return "";
  94.         }
  95.     }
  96.  
  97.     public static String takeSentence(final byte source){
  98.         String inPath;
  99.         String sentence;
  100.         if (source == 1) {
  101.             System.out.print("Enter the sentence: ");
  102.             sentence = scan.nextLine();
  103.         } else {
  104.             inPath = takeInPath();
  105.             sentence = takeSentenceFromFile(inPath);
  106.             if (sentence.equals("")){
  107.  
  108.                 inPath = takeInPath();
  109.                 sentence = takeSentenceFromFile(inPath);
  110.             }
  111.         }
  112.         return sentence;
  113.     }
  114.  
  115.     public static String makeSentenceNormal(String sentence){
  116.         sentence = sentence.replace('.', ' ');
  117.         sentence = sentence.replace(',', ' ');
  118.         sentence = sentence.replace('-', ' ');
  119.         sentence = sentence.replace(';', ' ');
  120.         sentence = sentence.replace('!', ' ');
  121.         sentence = sentence.replace('?', ' ');
  122.         sentence = sentence.replace(':', ' ');
  123.         sentence = sentence.replace('—', ' ');
  124.         sentence = sentence.trim();
  125.         return sentence;
  126.     }
  127.  
  128.     public static String[] findArrayOfWords(String sentence){
  129.         sentence = makeSentenceNormal(sentence);
  130.         String[] arrayOfWords = new String[100];
  131.         int wordsCounter = 0;
  132.         String word;
  133.         int index = sentence.indexOf(" ");
  134.         while (index != -1) {
  135.             word = sentence.substring(0, index);
  136.             if (!word.equals("")) {
  137.                 arrayOfWords[wordsCounter] = word;
  138.                 wordsCounter++;
  139.             }
  140.             sentence = sentence.substring(index + 1);
  141.             index = sentence.indexOf(" ");
  142.         }
  143.         if (!sentence.equals("")) {
  144.             arrayOfWords[wordsCounter] = sentence;
  145.             wordsCounter++;
  146.         }
  147.         String[] totalArrayOfWords = new String[wordsCounter];
  148.         for (int i = 0; i < wordsCounter; i++) {
  149.             totalArrayOfWords[i] = arrayOfWords[i];
  150.         }
  151.         return totalArrayOfWords;
  152.     }
  153.  
  154.     public static int findAnswer(String[] array){
  155.         int counter = 0;
  156.         for(int i = 0; i < array.length; i++){
  157.             if (array[i].equalsIgnoreCase(array[array.length - 1])){
  158.                 counter++;
  159.             }
  160.         }
  161.         return counter;
  162.     }
  163.  
  164.     public static void outputToFile(final String path, final  String sentence, final int answer){
  165.         try(FileWriter fw = new FileWriter(path)) {
  166.             fw.write("Sentence is:\n" + sentence + "\nLast word founded " + answer + " times.");
  167.             System.out.println("Done");
  168.         } catch (Exception e) {
  169.             System.out.println("File error!");
  170.         }
  171.     }
  172.  
  173.     public static void output(final byte source,final String sentence, final int answer){
  174.         String outPath;
  175.         if (source == 1) {
  176.             System.out.println("Sentence is:\n" + sentence + "\nLast word founded " + answer + " times.");
  177.         } else{
  178.             outPath = takeOutPath();
  179.             outputToFile(outPath, sentence ,answer);
  180.         }
  181.     }
  182. }
Add Comment
Please, Sign In to add comment