Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. import static java.lang.Integer.parseInt;
  7.  
  8. public class Class {
  9. public static void main(String[] args) throws IOException {
  10. int[] numArray = new int[0];
  11. numArray = programInput(numArray);
  12. Class.programOutput(numArray);
  13. }
  14.  
  15. private static int[] programInput(int[] numArray) throws IOException {
  16. boolean isInputCorrect = false;
  17. while (!isInputCorrect) {
  18. boolean isInputMethod = isMakeChoice();
  19. if (isInputMethod) {
  20. numArray = consoleInput();
  21. isInputCorrect = true;
  22. } else {
  23. numArray = fileInput();
  24. isInputCorrect = true;
  25. }
  26. }
  27. return numArray;
  28. }
  29.  
  30. private static void programOutput(int[] numArray) throws IOException {
  31. boolean outputCorrect = false;
  32. while (!outputCorrect) {
  33. boolean outputChoice = isMakeChoice();
  34. if (outputChoice) {
  35. programAnswerShow(numArray);
  36. outputCorrect = true;
  37. } else {
  38. fileOutput(numArray);
  39. outputCorrect = true;
  40. }
  41. }
  42. }
  43.  
  44. private static void fileOutput(int[] numArray) throws IOException {
  45. Scanner in = new Scanner(System.in);
  46. System.out.println("Введите путь к файлу:");
  47. boolean correct = false;
  48. do {
  49. String filePath = in.nextLine();
  50. try {
  51. FileWriter output = new FileWriter(filePath);
  52. output.write("Ваш массив: " + changeIntArrayToString(numArray));
  53. output.write("\n" + "Среднее арифметическое: " + arrayMidSum(numArray));
  54. output.close();
  55. } catch (FileNotFoundException fnf) {
  56. System.out.println("Не удается найти указанный файл, повторите попытку:");
  57. correct = true;
  58. }
  59. } while (correct);
  60. }
  61.  
  62. private static boolean isMakeChoice() {
  63. System.out.println("Введите 1 для ввода с консоли или 2, чтобы использовать файл. ");
  64. Scanner in = new Scanner(System.in);
  65. boolean correct;
  66. String choiceNum;
  67. do {
  68. choiceNum = in.nextLine();
  69. correct = !choiceNum.equalsIgnoreCase("1") && !choiceNum.equalsIgnoreCase("2");
  70. if (correct) {
  71. System.out.println("Вы можете ввести только 1 или 2, повторите попытку:");
  72. }
  73. } while (correct);
  74. boolean choice = false;
  75. if (choiceNum.equalsIgnoreCase("1")) {
  76. choice = true;
  77. }
  78. return choice;
  79. }
  80.  
  81. private static boolean isIncorrectInput(String strArray) {
  82. boolean result = false;
  83. if (strArray.charAt(0) != ' ' && !strArray.contains(",,") && strArray.charAt(0) != ',') {
  84. strArray = commaDelete(strArray);
  85. strArray = spaceDelete(strArray);
  86. try {
  87. parseInt(strArray);
  88. } catch (NumberFormatException numF) {
  89. result = true;
  90. }
  91. }
  92. return result;
  93. }
  94.  
  95. private static String spaceDelete(String strArray) {
  96. StringBuilder newStrArray = new StringBuilder();
  97. for (int i = 0; i < strArray.length(); i++) {
  98. if (strArray.charAt(i) != ' ') {
  99. newStrArray.append(strArray.charAt(i));
  100. }
  101. }
  102. return newStrArray.toString();
  103. }
  104.  
  105. private static String commaDelete(String strArray) {
  106. StringBuilder newStrArray = new StringBuilder();
  107. for (int i = 0; i < strArray.length(); i++) {
  108. if (strArray.charAt(i) != ',') {
  109. newStrArray.append(strArray.charAt(i));
  110. }
  111. }
  112. return newStrArray.toString();
  113. }
  114.  
  115. private static int[] consoleInput() {
  116. System.out.println("Введите элементы массива через запятую:");
  117. Scanner arrayIn = new Scanner(System.in);
  118. String strArray;
  119. boolean correct;
  120. do {
  121. strArray = arrayIn.nextLine();
  122. correct = isIncorrectInput(strArray);
  123. if (correct) {
  124. System.out.println("Перепроверьте введенные данные и повторите попытку:");
  125. correct = true;
  126. }
  127. } while (correct);
  128. strArray = spaceDelete(strArray);
  129. return changeStringToIntArray(strArray);
  130. }
  131.  
  132. private static int[] fileInput() throws IOException {
  133. Scanner arrayIn = new Scanner(System.in);
  134. System.out.println("Введите путь к файлу:");
  135. String strArray = "";
  136. boolean correct;
  137. do {
  138. String filePath = arrayIn.nextLine();
  139. try {
  140. BufferedReader in = new BufferedReader(new FileReader(filePath));
  141. strArray = in.readLine();
  142. in.close();
  143. correct = isIncorrectInput(strArray);
  144. if (correct) {
  145. System.out.println("Некорректные данные файла, исправьте ошибку и повторите попытку:");
  146. correct = true;
  147. }
  148. } catch (FileNotFoundException e) {
  149. System.out.println("Перепроверьте введенный путь и повторите попытку:");
  150. correct = true;
  151. } catch (NullPointerException nullP) {
  152. System.out.println("Вы ввели пустой файл, проверьте введенные данные и повторите попытку:");
  153. correct = true;
  154. }
  155. } while (correct || isIncorrectInput(strArray));
  156. strArray = spaceDelete(strArray);
  157. return changeStringToIntArray(strArray);
  158. }
  159.  
  160. private static int[] changeStringToIntArray(String strArray) {
  161. String[] array = strArray.split(",");
  162. int[] numArray = new int[array.length];
  163. for (int i = 0; i < array.length; i++) {
  164. numArray[i] = parseInt(array[i]);
  165. }
  166. return numArray;
  167. }
  168.  
  169. private static void programAnswerShow(int[] numArray) {
  170. System.out.print("Ваш массив: ");
  171. System.out.println(changeIntArrayToString(numArray));
  172. System.out.print("Среднее арифметическое = " + arrayMidSum(numArray));
  173. }
  174.  
  175. private static String changeIntArrayToString(int[] array) {
  176. StringBuilder str = new StringBuilder();
  177. for (int value : array) {
  178. str.append(value);
  179. str.append("; ");
  180. }
  181. return str.toString();
  182. }
  183.  
  184. private static double arrayMidSum(int[] array) {
  185. double sum = 0;
  186. int count = 0;
  187. for (int value : array) {
  188. sum += value;
  189. count++;
  190. }
  191. return sum / count;
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement