Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- static void condition(){
- System.out.println("This program converts a string according to the rule: ");
- System.out.println("1)If the string length > N, then the program will discard the first characters.");
- System.out.println("2)If the string length < N, then the program will add '.'");
- }
- static String inputFromFile() throws IOException {
- final int min = 1;
- final int max = 50;
- Scanner input = new Scanner(System.in);
- String sentence = "";
- String inputFile;
- boolean isNotCorrect;
- do {
- System.out.println("Enter the name of the file from which you want to read information: ");
- inputFile = input.nextLine();
- inputFile += ".txt";
- Scanner in = new Scanner(new File(inputFile));
- isNotCorrect = false;
- while (in.hasNext())
- sentence = sentence + in.nextLine();
- in.close();
- if((sentence.length() < min) || (sentence.length() > max)){
- isNotCorrect = true;
- System.out.println("Enter string length from interval [" + min + "..." + max + "]: ");
- }
- }while (isNotCorrect);
- System.out.println("Source string: " + sentence);
- return sentence;
- }
- static char choiseInput(){
- Scanner in = new Scanner(System.in);
- char input;
- boolean isNotCorrect;
- do {
- input = in.next().charAt(0);;
- input = (Character.toUpperCase(input));
- if ((input == 'Y') || (input == 'N')) {
- isNotCorrect = false;
- }
- else{
- isNotCorrect = true;
- System.out.println("Incorrect input. Enter Y(Yes) or N(No): ");
- }
- }while (isNotCorrect);
- return input;
- }
- static String checkInputString() {
- Scanner in = new Scanner(System.in);
- final int min = 1;
- final int max = 50;
- boolean isNotCorrect;
- String sentence;
- do {
- sentence = in.nextLine();
- if ((sentence.length() >= min) && (sentence.length() <= max)) {
- isNotCorrect = false;
- } else {
- System.out.println("Enter string length from interval [" + min + "..." + max + "]: ");
- isNotCorrect = true;
- }
- }while (isNotCorrect);
- return sentence;
- }
- static String inputFromConsole() {
- String sentence;
- System.out.println("Please, enter a string: ");
- sentence = checkInputString();
- return sentence;
- }
- static int CheckInput() {
- Scanner in = new Scanner(System.in);
- final int min = 1;
- final int max = 50;
- int n;
- boolean isNotCorrect;
- do {
- n = in.nextInt();
- if ((n >= min) && (n <= max)) {
- isNotCorrect = false;
- }
- else {
- isNotCorrect = true;
- System.out.println("Enter number from interval [" + min + "..." + max + "]: ");
- }
- }while (isNotCorrect);
- return n;
- }
- static void stringConversion(String sentence, int n) {
- int i;
- int len;
- char[] split;
- len = sentence.length();
- split = sentence.toCharArray();
- if (len > n) {
- System.out.println("Final string: ");
- for (i = len - n; i < len; i++) {
- System.out.print(split[i]);
- }
- System.out.println();
- }
- if (len < n) {
- for (i = len + 1; i <= n; i++) {
- sentence = "." + sentence;
- }
- System.out.println("Final string: " + sentence);
- }
- if (len == n) {
- System.out.println("Final string: " + sentence);
- }
- }
- static void outputInFile(String sentence) throws IOException {
- Scanner in = new Scanner(System.in);
- int i;
- String outputFileName;
- System.out.println("Enter the file name for the entry: ");
- outputFileName = in.nextLine();
- outputFileName = outputFileName + ".txt";
- FileWriter filewriter = new FileWriter(new File(outputFileName));
- filewriter.write("Final string: ");
- filewriter.write(sentence);
- filewriter.flush();
- }
- public static void main(String[] args) throws IOException {
- String sentence;
- int n;
- boolean isNotCorrect = false;
- condition();
- System.out.println("Would you like to use File input instead of Console input? Enter Y(Yes) or N(No): ");
- if (choiseInput() == 'N') {
- sentence = inputFromConsole();
- System.out.println("Enter N: ");
- n = CheckInput();
- stringConversion(sentence, n);
- System.out.println("Would you like to write down the answer to File? Enter Y(Yes) or N(No): ");
- if (choiseInput() == 'Y') {
- outputInFile(sentence);
- System.out.println("Program end");
- }else{
- System.out.println("Program end");
- }
- }else{
- do{
- try {
- isNotCorrect = false;
- sentence = inputFromFile();
- System.out.println("Enter N: ");
- n = CheckInput();
- stringConversion(sentence,n);
- System.out.println("Would you like to write down the answer to File? Enter Y(Yes) or N(No): ");
- if (choiseInput() == 'Y') {
- outputInFile(sentence);
- System.out.println("Program end");
- }else{
- System.out.println("Program end");
- }
- }catch (FileNotFoundException e){
- isNotCorrect = true;
- System.out.println("Mistake! The specified file was not found");
- }
- }while (isNotCorrect);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement