Advertisement
venik2405

lab3_1

Dec 9th, 2020
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.HashSet;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     static Scanner scConsole = new Scanner(System.in);
  8.  
  9.     private static PrintWriter getOutputFileLocation() {
  10.         boolean isIncorrect;
  11.         String location;
  12.         PrintWriter file = null;
  13.         do {
  14.             isIncorrect = false;
  15.             System.out.println("Enter file location:");
  16.             location = scConsole.nextLine();
  17.             try {
  18.                 file = new PrintWriter(location);
  19.             } catch (FileNotFoundException e) {
  20.                 isIncorrect = true;
  21.                 System.out.println("File with this location is not found");
  22.             }
  23.         } while (isIncorrect);
  24.         if (file != null)
  25.             System.out.println("File opened successfully");
  26.         return file;
  27.     }
  28.  
  29.     private static Scanner getInputFileLocation() {
  30.         boolean isIncorrect;
  31.         String location;
  32.         Scanner file = null;
  33.         do {
  34.             isIncorrect = false;
  35.             System.out.println("Enter file location:");
  36.             location = scConsole.nextLine();
  37.             try {
  38.                 file = new Scanner(new File(location));
  39.             } catch (FileNotFoundException e) {
  40.                 isIncorrect = true;
  41.                 System.out.println("File with this location is not found");
  42.             }
  43.         } while (isIncorrect);
  44.         if (file != null)
  45.             System.out.println("File opened successfully");
  46.         return file;
  47.     }
  48.  
  49.     public static String getLineFromFile() {
  50.         String str = null;
  51.         Scanner file = getInputFileLocation();
  52.         boolean isIncorrect = false;
  53.         do {
  54.             try {
  55.                 str = file.nextLine();
  56.             } catch (Exception e) {
  57.                 isIncorrect = true;
  58.                 System.out.println("Enter natural number");
  59.             }
  60.             if ((!isIncorrect) && (str.length() == 0)) {
  61.                 System.out.println("The line is empty");
  62.                 isIncorrect = true;
  63.             }
  64.         } while (isIncorrect);
  65.         return str;
  66.     }
  67.  
  68.  
  69.     private static String getLineFromConsole() {
  70.         String str = null;
  71.         boolean isIncorrect;
  72.         do {
  73.             isIncorrect = false;
  74.             try {
  75.                 str = scConsole.nextLine();
  76.             } catch (Exception e) {
  77.                 System.out.println("Enter the number");
  78.                 isIncorrect = true;
  79.             }
  80.             if (str.length() == 0) {
  81.                 System.out.println("You entered an empty line!\nRepeat enter");
  82.                 isIncorrect = true;
  83.             }
  84.         } while (isIncorrect);
  85.         return str;
  86.     }
  87.  
  88.     private static void outputToFile(int wordPosition) {
  89.         PrintWriter out = getOutputFileLocation();
  90.         out.print(wordPosition);
  91.         out.close();
  92.     }
  93.  
  94.     private static int chooseInput() {
  95.         boolean isIncorrect;
  96.         String line;
  97.         do {
  98.             isIncorrect = false;
  99.             System.out.println("Do you want to input from file? (y/n)");
  100.             line = scConsole.nextLine().toLowerCase();
  101.             if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
  102.                 isIncorrect = true;
  103.                 System.out.println("Enter valid answer");
  104.             }
  105.         } while (isIncorrect);
  106.         if (line.equals("y") || line.equals("")) {
  107.             return 0;
  108.         } else {
  109.             return 1;
  110.         }
  111.     }
  112.  
  113.     private static int findAmountOfWords(String str) {
  114.         int amount = 1;
  115.         for (int i = 0; i < str.length(); i++) {
  116.             if (str.charAt(i) == ' ') {
  117.                 amount++;
  118.             }
  119.         }
  120.         return amount;
  121.     }
  122.  
  123.     private static String[] findWords(String str, int amount) {
  124.         int j = 0;
  125.         String[] words = new String[amount];
  126.         for (int i = 0; i < amount; i++) {
  127.             words[i] = "";
  128.             while ((j < str.length()) && (str.charAt(j) != ' ')) {
  129.                 words[i] = words[i] + str.charAt(j);
  130.                 j++;
  131.             }
  132.             j++;
  133.         }
  134.         return words;
  135.     }
  136.  
  137.     private static int findShortestWord(int amount, String[] words) {
  138.         int minLength = words[0].length();
  139.         int pos = 0;
  140.         for (int i = 0; i < amount; i++) {
  141.             if (minLength > words[i].length()) {
  142.                 minLength = words[i].length();
  143.                 pos = i;
  144.             }
  145.         }
  146.         return pos;
  147.     }
  148.  
  149.     private static int findPos(String[] words, int pos){
  150.         int wordPosition = 0;
  151.         int i = 0;
  152.         for (i = 0;i < pos; i++){
  153.             wordPosition = wordPosition + words[i].length();
  154.         }
  155.         wordPosition = wordPosition + i;
  156.         return wordPosition;
  157.     }
  158.  
  159.     public static void main(String[] args) {
  160.         String str;
  161.         System.out.println("Программа находит самое короткое слово в предложении и указывает позицию , с которой оно начинается.");
  162.         int chosenInput = chooseInput();
  163.         System.out.println("Enter the string");
  164.         if (chosenInput == 0) {
  165.             str = getLineFromFile();
  166.         } else {
  167.             str = getLineFromConsole();
  168.         }
  169.         int amount = findAmountOfWords(str);
  170.         String[] words = findWords(str, amount);
  171.         int pos = findShortestWord(amount, words);
  172.         int wordPosition = findPos(words,pos);
  173.         System.out.println("Позиция самого короткого слова в предлоении: " + wordPosition);
  174.         outputToFile(wordPosition);
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement