SHARE
TWEET

Untitled

a guest Nov 12th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.util.Scanner;
  7. public class Main {
  8.     static void condition(){
  9.         System.out.println("This program converts a string according to the rule: ");
  10.         System.out.println("1)If the string length > N, then the program will discard the first characters.");
  11.         System.out.println("2)If the string length < N, then the program will add '.'");
  12.     }
  13.     static String inputFromFile() throws IOException {
  14.         final int min = 1;
  15.         final int max = 50;
  16.         Scanner input = new Scanner(System.in);
  17.         String sentence = "";
  18.         String inputFile;
  19.         boolean isNotCorrect;
  20.         do {
  21.             System.out.println("Enter the name of the file from which you want to read information: ");
  22.             inputFile = input.nextLine();
  23.             inputFile += ".txt";
  24.             Scanner in = new Scanner(new File(inputFile));
  25.             isNotCorrect = false;
  26.             while (in.hasNext())
  27.                 sentence = sentence + in.nextLine();
  28.             in.close();
  29.             if((sentence.length() < min) || (sentence.length() > max)){
  30.                 isNotCorrect = true;
  31.                 System.out.println("Enter string length from interval [" + min + "..." + max + "]: ");
  32.             }
  33.         }while (isNotCorrect);
  34.         System.out.println("Source string: " + sentence);
  35.         return sentence;
  36.     }
  37.     static char choiseInput(){
  38.         Scanner in = new Scanner(System.in);
  39.         char input;
  40.         boolean isNotCorrect;
  41.         do {
  42.             input = in.next().charAt(0);;
  43.             input = (Character.toUpperCase(input));
  44.             if ((input == 'Y') || (input == 'N')) {
  45.                 isNotCorrect = false;
  46.             }
  47.             else{
  48.                 isNotCorrect = true;
  49.                 System.out.println("Incorrect input. Enter Y(Yes) or N(No): ");
  50.                 }
  51.         }while (isNotCorrect);
  52.         return input;
  53.     }
  54.     static String checkInputString() {
  55.         Scanner in = new Scanner(System.in);
  56.         final int min = 1;
  57.         final int max = 50;
  58.         boolean isNotCorrect;
  59.         String sentence;
  60.         do {
  61.             sentence = in.nextLine();
  62.             if ((sentence.length() >= min) && (sentence.length() <= max)) {
  63.                 isNotCorrect = false;
  64.             } else {
  65.                 System.out.println("Enter string length from interval [" + min + "..." + max + "]: ");
  66.                 isNotCorrect = true;
  67.             }
  68.         }while (isNotCorrect);
  69.         return sentence;
  70.     }
  71.     static String inputFromConsole() {
  72.         String sentence;
  73.         System.out.println("Please, enter a string: ");
  74.         sentence = checkInputString();
  75.         return sentence;
  76.     }
  77.     static int CheckInput() {
  78.         Scanner in = new Scanner(System.in);
  79.         final int min = 1;
  80.         final int max = 50;
  81.         int n;
  82.         boolean isNotCorrect;
  83.         do {
  84.             n = in.nextInt();
  85.             if ((n >= min) && (n <= max)) {
  86.                 isNotCorrect = false;
  87.             }
  88.             else {
  89.                 isNotCorrect = true;
  90.                 System.out.println("Enter number from interval [" + min + "..." + max + "]: ");
  91.             }
  92.         }while (isNotCorrect);
  93.         return n;
  94.     }
  95.     static void stringConversion(String sentence, int n) {
  96.         int i;
  97.         int len;
  98.         char[] split;
  99.         len = sentence.length();
  100.         split = sentence.toCharArray();
  101.         if (len > n) {
  102.             System.out.println("Final string: ");
  103.             for (i = len - n; i < len; i++) {
  104.                 System.out.print(split[i]);
  105.             }
  106.             System.out.println();
  107.         }
  108.         if (len < n) {
  109.             for (i = len + 1; i <= n; i++) {
  110.                 sentence = "." + sentence;
  111.             }
  112.             System.out.println("Final string: " + sentence);
  113.         }
  114.         if (len == n) {
  115.             System.out.println("Final string: " + sentence);
  116.         }
  117.     }
  118.     static void outputInFile(String sentence) throws IOException {
  119.         Scanner in = new Scanner(System.in);
  120.         int i;
  121.         String outputFileName;
  122.         System.out.println("Enter the file name for the entry: ");
  123.          outputFileName = in.nextLine();
  124.          outputFileName = outputFileName + ".txt";
  125.          FileWriter filewriter = new FileWriter(new File(outputFileName));
  126.          filewriter.write("Final string: ");
  127.          filewriter.write(sentence);
  128.          filewriter.flush();
  129.          }
  130.     public static void main(String[] args) throws IOException {
  131.         String sentence;
  132.         int n;
  133.         boolean isNotCorrect = false;
  134.         condition();
  135.         System.out.println("Would you like to use File input instead of Console input? Enter Y(Yes) or N(No): ");
  136.         if (choiseInput() == 'N') {
  137.             sentence = inputFromConsole();
  138.             System.out.println("Enter N: ");
  139.             n = CheckInput();
  140.             stringConversion(sentence, n);
  141.             System.out.println("Would you like to write down the answer to File? Enter Y(Yes) or N(No): ");
  142.             if (choiseInput() == 'Y') {
  143.                 outputInFile(sentence);
  144.                 System.out.println("Program end");
  145.             }else{
  146.                 System.out.println("Program end");
  147.             }
  148.         }else{
  149.             do{
  150.                 try {
  151.                     isNotCorrect = false;
  152.                     sentence = inputFromFile();
  153.                     System.out.println("Enter N: ");
  154.                     n = CheckInput();
  155.                     stringConversion(sentence,n);
  156.                     System.out.println("Would you like to write down the answer to File? Enter Y(Yes) or N(No): ");
  157.                     if (choiseInput() == 'Y') {
  158.                         outputInFile(sentence);
  159.                         System.out.println("Program end");
  160.                     }else{
  161.                         System.out.println("Program end");
  162.                     }
  163.                 }catch (FileNotFoundException e){
  164.                     isNotCorrect = true;
  165.                     System.out.println("Mistake! The specified file was not found");
  166.                 }
  167.             }while (isNotCorrect);
  168.         }
  169.     }
  170. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top