Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.Scanner;
- public class lab3_1 {
- static Scanner scan = new Scanner(System.in);
- public static void main(String[] args){
- 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: ");
- byte inputSource = takeSource();
- String sentence = takeSentence(inputSource);
- String[] arrayOfWords = findArrayOfWords(sentence);
- int answer = findAnswer(arrayOfWords);
- System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
- byte outputSource = takeSource();
- output(outputSource, sentence, answer);
- scan.close();
- }
- public static byte takeSource(){
- boolean isIncorrect;
- byte choice = 0;
- final byte CONSOLE = 1;
- final byte FILE = 2;
- do {
- isIncorrect = false;
- try {
- choice = Byte.parseByte(scan.nextLine());
- } catch (Exception e) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
- System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- public static String takeInPath(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (Files.notExists(Path.of(path))) {
- System.out.print("File is not found\nEnter file path: ");
- isIncorrect = true;
- }
- if (!isIncorrect && (!path.endsWith(".txt"))) {
- System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- public static String takeOutPath(){
- String path;
- boolean isIncorrect;
- System.out.print("Enter file path: ");
- do {
- isIncorrect = false;
- path = scan.nextLine();
- if (!path.endsWith(".txt")) {
- System.out.print("It should be a \".txt\" file\nEnter file path: ");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- public static String takeSentenceFromFile(final String path){
- String sentence = "";
- boolean isCorrect = true;
- try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
- sentence = reader.readLine();
- if ((reader.readLine()) != null){
- isCorrect = false;
- System.out.println("Incorrect file data!!");
- }
- }catch (IOException e) {
- System.out.println("Input/Output error!!!");
- isCorrect = false;
- }
- if(isCorrect) {
- return sentence;
- }else{
- return "";
- }
- }
- public static String takeSentence(final byte source){
- String inPath;
- String sentence;
- if (source == 1) {
- System.out.print("Enter the sentence: ");
- sentence = scan.nextLine();
- } else {
- inPath = takeInPath();
- sentence = takeSentenceFromFile(inPath);
- if (sentence.equals("")){
- inPath = takeInPath();
- sentence = takeSentenceFromFile(inPath);
- }
- }
- return sentence;
- }
- public static String makeSentenceNormal(String sentence){
- sentence = sentence.replace('.', ' ');
- sentence = sentence.replace(',', ' ');
- sentence = sentence.replace('-', ' ');
- sentence = sentence.replace(';', ' ');
- sentence = sentence.replace('!', ' ');
- sentence = sentence.replace('?', ' ');
- sentence = sentence.replace(':', ' ');
- sentence = sentence.replace('—', ' ');
- sentence = sentence.trim();
- return sentence;
- }
- public static String[] findArrayOfWords(String sentence){
- sentence = makeSentenceNormal(sentence);
- String[] arrayOfWords = new String[100];
- int wordsCounter = 0;
- String word;
- int index = sentence.indexOf(" ");
- while (index != -1) {
- word = sentence.substring(0, index);
- if (!word.equals("")) {
- arrayOfWords[wordsCounter] = word;
- wordsCounter++;
- }
- sentence = sentence.substring(index + 1);
- index = sentence.indexOf(" ");
- }
- if (!sentence.equals("")) {
- arrayOfWords[wordsCounter] = sentence;
- wordsCounter++;
- }
- String[] totalArrayOfWords = new String[wordsCounter];
- for (int i = 0; i < wordsCounter; i++) {
- totalArrayOfWords[i] = arrayOfWords[i];
- }
- return totalArrayOfWords;
- }
- public static int findAnswer(String[] array){
- int counter = 0;
- for(int i = 0; i < array.length; i++){
- if (array[i].equalsIgnoreCase(array[array.length - 1])){
- counter++;
- }
- }
- return counter;
- }
- public static void outputToFile(final String path, final String sentence, final int answer){
- try(FileWriter fw = new FileWriter(path)) {
- fw.write("Sentence is:\n" + sentence + "\nLast word founded " + answer + " times.");
- System.out.println("Done");
- } catch (Exception e) {
- System.out.println("File error!");
- }
- }
- public static void output(final byte source,final String sentence, final int answer){
- String outPath;
- if (source == 1) {
- System.out.println("Sentence is:\n" + sentence + "\nLast word founded " + answer + " times.");
- } else{
- outPath = takeOutPath();
- outputToFile(outPath, sentence ,answer);
- }
- }
- }
Add Comment
Please, Sign In to add comment