Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Main {
- public static Scanner scan = new Scanner(System.in);
- public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! A file" +
- "with the same name was not found. Try again.";
- public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to " +
- "open this file.Please check the file and try again.\n";
- public static final String THEME_MESSAGE = "This program implements Shell sorting algorithm";
- public static final String DOTTED_LINE = "------------------------------------------------";
- public static final String CHOOSE_INPUT = "If you want to fill the array from the keyboard, please, enter 'K'," +
- " if you want to use the file, please, enter 'F'";
- public static final String INPUT_ARRAY_LENGTH = "Please input the length of the array. (an integer value from 2 to 10)";
- public static final String SMT_WRONG = "Something wrong, please try again";
- public static final String NOT_INVALID_DATA = "The length does not match the elements of the array, please check the file for correctness";
- public static final String INPUT_FILE_NAME = "Please enter the name of the file from which to read data. \n" +
- "For example,\"C:\\Users\\Eugene\\Desktop\\Name.txt";
- public static final String OUTPUT_FILE_NAME = "Please enter the name of the file which you want to write the result. \n" +
- "For example,\"C:\\Users\\Eugene\\Desktop\\Name.txt";
- public static final String CHOOSE_OUTPUT = "\nWould you like to put the result in a file? ('Y' - save, 'N' - don't save)";
- public static final String NOT_CORRECT_INPUT = "Attention, an error occurred while Input. Please try again";
- public enum typeOfInput {
- CHOOSE_KEYBOARD, CHOOSE_FILE;
- }
- public enum typeOfOutput {
- YES, NO;
- }
- public static void main(String[] args) {
- System.out.println(THEME_MESSAGE + "\n" + DOTTED_LINE);
- int lengthArray;
- int[] arrayNumbers = new int[0];
- typeOfInput Input = chooseInput();
- switch (Input) {
- case CHOOSE_KEYBOARD:
- System.out.println(INPUT_ARRAY_LENGTH);
- lengthArray = checkInputNumbers(1, 20);
- arrayNumbers = new int[lengthArray];
- inputArrayFromKeyboard(arrayNumbers, lengthArray);
- break;
- case CHOOSE_FILE:
- arrayNumbers = inputArrayFromFile();
- System.out.println("Entered matrix:");
- outputInConsole(arrayNumbers);
- break;
- }
- sortingArray(arrayNumbers);
- outputInConsole(arrayNumbers);
- typeOfOutput Output = chooseOutput();
- switch (Output) {
- case YES:
- outputResultToFile(arrayNumbers);
- break;
- case NO:
- break;
- }
- System.out.println(DOTTED_LINE + "\nProgram completed.");
- }
- private static void sortingArray(int[] arr) {
- for (int inc = arr.length / 2; inc >= 1; inc = inc / 2)
- for (int step = 0; step < inc; step++)
- insertionSort(arr, step, inc);
- }
- private static void insertionSort(int[] arr, int start, int inc) {
- int temp;
- for (int i = start; i < arr.length - 1; i += inc)
- for (int j = Math.min(i + inc, arr.length - 1); j - inc >= 0; j = j - inc)
- if (arr[j - inc] > arr[j]) {
- temp = arr[j];
- arr[j] = arr[j - inc];
- arr[j - inc] = temp;
- } else break;
- }
- public static void inputArrayFromKeyboard(int[] array, int arraylength) {
- System.out.println("Please enter the array elements in the range from -10_000 to 10_000");
- for (int i = 0; i < arraylength; i++) {
- System.out.print("Array[" + (i) + "]" + " = ");
- array[i] = checkInputNumbers(-10_000, 10_000);
- }
- }
- public static boolean checkInputParameters(int num, int min, int max) {
- return ((num > min) && (num < max));
- }
- public static int checkInputNumbers(int min, int max) {
- boolean isCorrect = true;
- int number = 0;
- Scanner scan = new Scanner(System.in);
- do {
- try {
- number = scan.nextInt();
- if (checkInputParameters(number, min - 1, max + 1)) {
- isCorrect = false;
- } else {
- System.out.println("Error! Enter a number. The number " +
- "must be between " + min + " and max " + "!");
- }
- } catch (Exception e) {
- System.out.println("Error! Enter a number: ");
- scan.next();
- }
- } while (isCorrect);
- return number;
- }
- public static void outputInConsole(int arr[]) {
- System.out.println("\nResult sorting array:");
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i] + "\t");
- }
- }
- public static boolean checkSymbol(char symbol, char firstSymbol, char secondSymbol){
- return ((symbol != firstSymbol) & (symbol != secondSymbol));
- }
- public static typeOfInput chooseInput() {
- typeOfInput answer = null;
- boolean isNotCorrect;
- do {
- System.out.println(CHOOSE_INPUT);
- char symbol = scan.next().charAt(0);
- symbol = Character.toUpperCase(symbol);
- isNotCorrect = (checkSymbol(symbol, 'K', 'F'));
- if (symbol == 'K') {
- answer = typeOfInput.CHOOSE_KEYBOARD;
- } else if (symbol == 'F') {
- answer = typeOfInput.CHOOSE_FILE;
- }
- }
- while (isNotCorrect);
- return answer;
- }
- public static int[] inputArrayFromFile() {
- boolean isInvalidInput;
- int[] arrayNumbers = new int[0];
- scan.nextLine();
- do {
- System.out.println(INPUT_FILE_NAME);
- String fileName = scan.nextLine();
- try {
- BufferedReader input = new BufferedReader(new FileReader(fileName));
- String text = input.readLine();
- int lengthOfMatrix = Integer.parseInt(text);
- isInvalidInput = checkInputParameters(lengthOfMatrix, 2, 10);
- arrayNumbers = new int[lengthOfMatrix];
- text = input.readLine();
- String[] arrFilling = text.split(" ");
- for (int i = 0; i < lengthOfMatrix; i++) {
- arrayNumbers[i] = Integer.parseInt(arrFilling[i]);
- }
- input.close();
- } catch (ArrayIndexOutOfBoundsException e) {
- System.out.println(NOT_INVALID_DATA);
- isInvalidInput = true;
- } catch (FileNotFoundException e) {
- System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
- isInvalidInput = true;
- } catch (IOException e) {
- System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
- isInvalidInput = true;
- } catch (NumberFormatException e) {
- System.out.println("Error! The file contains invalid data. " +
- "Please check the file and try again.");
- isInvalidInput = true;
- } catch (Exception e) {
- System.out.println(SMT_WRONG);
- isInvalidInput = true;
- }
- } while (isInvalidInput);
- return arrayNumbers;
- }
- public static typeOfOutput chooseOutput() {
- typeOfOutput answer;
- boolean isNotCorrect;
- do {
- System.out.println(CHOOSE_OUTPUT);
- char symbol = scan.next().charAt(0);
- symbol = Character.toUpperCase(symbol);
- isNotCorrect = (checkSymbol(symbol, 'Y', 'N'));
- if (symbol == 'Y') {
- answer = typeOfOutput.YES;
- } else {
- answer = typeOfOutput.NO;
- }
- }
- while (isNotCorrect);
- return answer;
- }
- public static void outputResultToFile(int[] arrayNumbers) {
- boolean isInvalidOutput;
- scan.nextLine();
- do {
- isInvalidOutput = false;
- System.out.println(OUTPUT_FILE_NAME);
- String FileName = scan.nextLine();
- try {
- BufferedWriter output = new BufferedWriter(new FileWriter(FileName));
- output.write("Result sorting array: \n");
- for (int i : arrayNumbers) {
- output.write(arrayNumbers[i] + " ");
- }
- output.close();
- } catch (FileNotFoundException e) {
- System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
- isInvalidOutput = true;
- } catch (IOException e) {
- System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
- isInvalidOutput = true;
- }
- }
- while (isInvalidOutput);
- System.out.println("Data recording completed successfully.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement