Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.HashSet;
- import java.util.Scanner;
- public class Main {
- static Scanner scConsole = new Scanner(System.in);
- private static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- public static String getLineFromFile() {
- String str = null;
- Scanner file = getInputFileLocation();
- boolean isIncorrect = false;
- do {
- try {
- str = file.nextLine();
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Enter natural number");
- }
- if ((!isIncorrect) && (str.length() == 0)) {
- System.out.println("The line is empty");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- private static String getLineFromConsole() {
- String str = null;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- str = scConsole.nextLine();
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if (str.length() == 0) {
- System.out.println("You entered an empty line!\nRepeat enter");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return str;
- }
- private static void outputToFile(int wordPosition) {
- PrintWriter out = getOutputFileLocation();
- out.print(wordPosition);
- out.close();
- }
- private static int chooseInput() {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- private static int findAmountOfWords(String str) {
- int amount = 1;
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) == ' ') {
- amount++;
- }
- }
- return amount;
- }
- private static String[] findWords(String str, int amount) {
- int j = 0;
- String[] words = new String[amount];
- for (int i = 0; i < amount; i++) {
- words[i] = "";
- while ((j < str.length()) && (str.charAt(j) != ' ')) {
- words[i] = words[i] + str.charAt(j);
- j++;
- }
- j++;
- }
- return words;
- }
- private static int findShortestWord(int amount, String[] words) {
- int minLength = words[0].length();
- int pos = 0;
- for (int i = 0; i < amount; i++) {
- if (minLength > words[i].length()) {
- minLength = words[i].length();
- pos = i;
- }
- }
- return pos;
- }
- private static int findPos(String[] words, int pos){
- int wordPosition = 0;
- int i = 0;
- for (i = 0;i < pos; i++){
- wordPosition = wordPosition + words[i].length();
- }
- wordPosition = wordPosition + i;
- return wordPosition;
- }
- public static void main(String[] args) {
- String str;
- System.out.println("Программа находит самое короткое слово в предложении и указывает позицию , с которой оно начинается.");
- int chosenInput = chooseInput();
- System.out.println("Enter the string");
- if (chosenInput == 0) {
- str = getLineFromFile();
- } else {
- str = getLineFromConsole();
- }
- int amount = findAmountOfWords(str);
- String[] words = findWords(str, amount);
- int pos = findShortestWord(amount, words);
- int wordPosition = findPos(words,pos);
- System.out.println("Позиция самого короткого слова в предлоении: " + wordPosition);
- outputToFile(wordPosition);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement