Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. public static Scanner scan = new Scanner(System.in);
  6. public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! A file" +
  7. "with the same name was not found. Try again.";
  8. public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to " +
  9. "open this file.Please check the file and try again.\n";
  10. public static final String THEME_MESSAGE = "This program implements Shell sorting algorithm";
  11. public static final String DOTTED_LINE = "------------------------------------------------";
  12. public static final String CHOOSE_INPUT = "If you want to fill the array from the keyboard, please, enter 'K'," +
  13. " if you want to use the file, please, enter 'F'";
  14. public static final String INPUT_ARRAY_LENGTH = "Please input the length of the array. (an integer value from 2 to 10)";
  15. public static final String SMT_WRONG = "Something wrong, please try again";
  16. public static final String NOT_INVALID_DATA = "The length does not match the elements of the array, please check the file for correctness";
  17. public static final String INPUT_FILE_NAME = "Please enter the name of the file from which to read data. \n" +
  18. "For example,\"C:\\Users\\Eugene\\Desktop\\Name.txt";
  19. public static final String OUTPUT_FILE_NAME = "Please enter the name of the file which you want to write the result. \n" +
  20. "For example,\"C:\\Users\\Eugene\\Desktop\\Name.txt";
  21. public static final String CHOOSE_OUTPUT = "\nWould you like to put the result in a file? ('Y' - save, 'N' - don't save)";
  22. public static final String NOT_CORRECT_INPUT = "Attention, an error occurred while Input. Please try again";
  23.  
  24. public enum typeOfInput {
  25. CHOOSE_KEYBOARD, CHOOSE_FILE;
  26. }
  27.  
  28. public enum typeOfOutput {
  29. YES, NO;
  30. }
  31.  
  32. public static void main(String[] args) {
  33.  
  34. System.out.println(THEME_MESSAGE + "\n" + DOTTED_LINE);
  35. int lengthArray;
  36. int[] arrayNumbers = new int[0];
  37. typeOfInput Input = chooseInput();
  38. switch (Input) {
  39. case CHOOSE_KEYBOARD:
  40. System.out.println(INPUT_ARRAY_LENGTH);
  41. lengthArray = checkInputNumbers(1, 20);
  42. arrayNumbers = new int[lengthArray];
  43. inputArrayFromKeyboard(arrayNumbers, lengthArray);
  44. break;
  45. case CHOOSE_FILE:
  46. arrayNumbers = inputArrayFromFile();
  47. System.out.println("Entered matrix:");
  48. outputInConsole(arrayNumbers);
  49. break;
  50. }
  51. sortingArray(arrayNumbers);
  52. outputInConsole(arrayNumbers);
  53. typeOfOutput Output = chooseOutput();
  54. switch (Output) {
  55. case YES:
  56. outputResultToFile(arrayNumbers);
  57. break;
  58. case NO:
  59. break;
  60. }
  61. System.out.println(DOTTED_LINE + "\nProgram completed.");
  62. }
  63.  
  64. private static void sortingArray(int[] arr) {
  65. for (int inc = arr.length / 2; inc >= 1; inc = inc / 2)
  66. for (int step = 0; step < inc; step++)
  67. insertionSort(arr, step, inc);
  68. }
  69.  
  70. private static void insertionSort(int[] arr, int start, int inc) {
  71. int temp;
  72. for (int i = start; i < arr.length - 1; i += inc)
  73. for (int j = Math.min(i + inc, arr.length - 1); j - inc >= 0; j = j - inc)
  74. if (arr[j - inc] > arr[j]) {
  75. temp = arr[j];
  76. arr[j] = arr[j - inc];
  77. arr[j - inc] = temp;
  78. } else break;
  79. }
  80.  
  81. public static void inputArrayFromKeyboard(int[] array, int arraylength) {
  82. System.out.println("Please enter the array elements in the range from -10_000 to 10_000");
  83. for (int i = 0; i < arraylength; i++) {
  84. System.out.print("Array[" + (i) + "]" + " = ");
  85. array[i] = checkInputNumbers(-10_000, 10_000);
  86. }
  87. }
  88.  
  89. public static boolean checkInputParameters(int num, int min, int max) {
  90. return ((num > min) && (num < max));
  91. }
  92.  
  93. public static int checkInputNumbers(int min, int max) {
  94. boolean isCorrect = true;
  95. int number = 0;
  96. Scanner scan = new Scanner(System.in);
  97. do {
  98. try {
  99. number = scan.nextInt();
  100. if (checkInputParameters(number, min - 1, max + 1)) {
  101. isCorrect = false;
  102. } else {
  103. System.out.println("Error! Enter a number. The number " +
  104. "must be between " + min + " and max " + "!");
  105. }
  106. } catch (Exception e) {
  107. System.out.println("Error! Enter a number: ");
  108. scan.next();
  109. }
  110. } while (isCorrect);
  111. return number;
  112. }
  113.  
  114. public static void outputInConsole(int arr[]) {
  115. System.out.println("\nResult sorting array:");
  116. for (int i = 0; i < arr.length; i++) {
  117. System.out.print(arr[i] + "\t");
  118. }
  119. }
  120.  
  121. public static boolean checkSymbol(char symbol, char firstSymbol, char secondSymbol){
  122. return ((symbol != firstSymbol) & (symbol != secondSymbol));
  123. }
  124.  
  125. public static typeOfInput chooseInput() {
  126. typeOfInput answer = null;
  127. boolean isNotCorrect;
  128. do {
  129. System.out.println(CHOOSE_INPUT);
  130. char symbol = scan.next().charAt(0);
  131. symbol = Character.toUpperCase(symbol);
  132. isNotCorrect = (checkSymbol(symbol, 'K', 'F'));
  133. if (symbol == 'K') {
  134. answer = typeOfInput.CHOOSE_KEYBOARD;
  135. } else if (symbol == 'F') {
  136. answer = typeOfInput.CHOOSE_FILE;
  137. }
  138. }
  139. while (isNotCorrect);
  140. return answer;
  141. }
  142.  
  143. public static int[] inputArrayFromFile() {
  144. boolean isInvalidInput;
  145. int[] arrayNumbers = new int[0];
  146. scan.nextLine();
  147. do {
  148. System.out.println(INPUT_FILE_NAME);
  149. String fileName = scan.nextLine();
  150. try {
  151. BufferedReader input = new BufferedReader(new FileReader(fileName));
  152. String text = input.readLine();
  153. int lengthOfMatrix = Integer.parseInt(text);
  154. isInvalidInput = checkInputParameters(lengthOfMatrix, 2, 10);
  155. arrayNumbers = new int[lengthOfMatrix];
  156. text = input.readLine();
  157. String[] arrFilling = text.split(" ");
  158. for (int i = 0; i < lengthOfMatrix; i++) {
  159. arrayNumbers[i] = Integer.parseInt(arrFilling[i]);
  160. }
  161. input.close();
  162. } catch (ArrayIndexOutOfBoundsException e) {
  163. System.out.println(NOT_INVALID_DATA);
  164. isInvalidInput = true;
  165. } catch (FileNotFoundException e) {
  166. System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
  167. isInvalidInput = true;
  168. } catch (IOException e) {
  169. System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
  170. isInvalidInput = true;
  171. } catch (NumberFormatException e) {
  172. System.out.println("Error! The file contains invalid data. " +
  173. "Please check the file and try again.");
  174. isInvalidInput = true;
  175. } catch (Exception e) {
  176. System.out.println(SMT_WRONG);
  177. isInvalidInput = true;
  178. }
  179. } while (isInvalidInput);
  180. return arrayNumbers;
  181. }
  182.  
  183. public static typeOfOutput chooseOutput() {
  184. typeOfOutput answer;
  185. boolean isNotCorrect;
  186. do {
  187. System.out.println(CHOOSE_OUTPUT);
  188. char symbol = scan.next().charAt(0);
  189. symbol = Character.toUpperCase(symbol);
  190. isNotCorrect = (checkSymbol(symbol, 'Y', 'N'));
  191. if (symbol == 'Y') {
  192. answer = typeOfOutput.YES;
  193. } else {
  194. answer = typeOfOutput.NO;
  195. }
  196. }
  197. while (isNotCorrect);
  198. return answer;
  199. }
  200.  
  201. public static void outputResultToFile(int[] arrayNumbers) {
  202. boolean isInvalidOutput;
  203. scan.nextLine();
  204. do {
  205. isInvalidOutput = false;
  206. System.out.println(OUTPUT_FILE_NAME);
  207. String FileName = scan.nextLine();
  208. try {
  209. BufferedWriter output = new BufferedWriter(new FileWriter(FileName));
  210. output.write("Result sorting array: \n");
  211. for (int i : arrayNumbers) {
  212. output.write(arrayNumbers[i] + " ");
  213. }
  214. output.close();
  215. } catch (FileNotFoundException e) {
  216. System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
  217. isInvalidOutput = true;
  218. } catch (IOException e) {
  219. System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
  220. isInvalidOutput = true;
  221. }
  222. }
  223. while (isInvalidOutput);
  224. System.out.println("Data recording completed successfully.");
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement