Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 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 isCorrectInput(String strArray) {
  82. if (strArray.charAt(0) == ' ') {
  83. return true;
  84. }
  85. strArray = spaceDelete(strArray);
  86. if (strArray.contains(",,") || strArray.charAt(0) == ',') {
  87. return true;
  88. } else {
  89. strArray = commaDelete(strArray);
  90. try {
  91. parseInt(strArray);
  92. } catch (NumberFormatException numF) {
  93. return true;
  94. }
  95. return false;
  96. }
  97.  
  98. }
  99.  
  100. private static String spaceDelete(String strArray) {
  101. StringBuilder newStrArray = new StringBuilder();
  102. for (int i = 0; i < strArray.length(); i++) {
  103. if (strArray.charAt(i) != ' ') {
  104. newStrArray.append(strArray.charAt(i));
  105. }
  106. }
  107. return newStrArray.toString();
  108. }
  109.  
  110. private static String commaDelete(String strArray) {
  111. StringBuilder newStrArray = new StringBuilder();
  112. for (int i = 0; i < strArray.length(); i++) {
  113. if (strArray.charAt(i) != ',') {
  114. newStrArray.append(strArray.charAt(i));
  115. }
  116. }
  117. return newStrArray.toString();
  118. }
  119.  
  120. private static int[] consoleInput() {
  121. System.out.println("Введите элементы массива через запятую:");
  122. Scanner arrayIn = new Scanner(System.in);
  123. String strArray;
  124. boolean correct;
  125. do {
  126. strArray = arrayIn.nextLine();
  127. correct = isCorrectInput(strArray);
  128. if (correct) {
  129. System.out.println("Перепроверьте введенные данные и повторите попытку:");
  130. correct = true;
  131. }
  132. } while (correct);
  133. strArray = spaceDelete(strArray);
  134. return changeStringToIntArray(strArray);
  135.  
  136. }
  137.  
  138. private static int[] fileInput() throws IOException {
  139. Scanner arrayIn = new Scanner(System.in);
  140. System.out.println("Введите путь к файлу:");
  141. String strArray = "";
  142. boolean correct;
  143. do {
  144. String filePath = arrayIn.nextLine();
  145. try {
  146. BufferedReader in = new BufferedReader(new FileReader(filePath));
  147. strArray = in.readLine();
  148. in.close();
  149. correct = isCorrectInput(strArray);
  150. if (correct) {
  151. System.out.println("Некорректные данные файла, исправьте ошибку и повторите попытку:");
  152. correct = true;
  153. }
  154. } catch (FileNotFoundException e) {
  155. System.out.println("Перепроверьте введенный путь и повторите попытку:");
  156. correct = true;
  157. } catch (NullPointerException nullP) {
  158. System.out.println("Вы ввели пустой файл, проверьте введенные данные и повторите попытку:");
  159. correct = true;
  160. }
  161. } while (correct || isCorrectInput(strArray));
  162. strArray = spaceDelete(strArray);
  163. return changeStringToIntArray(strArray);
  164. }
  165.  
  166. private static int[] changeStringToIntArray(String strArray) {
  167. String[] array = strArray.split(",");
  168. int[] numArray = new int[array.length];
  169. for (int i = 0; i < array.length; i++) {
  170. numArray[i] = parseInt(array[i]);
  171. }
  172. return numArray;
  173. }
  174.  
  175. private static void programAnswerShow(int[] numArray) {
  176. System.out.print("Ваш массив: ");
  177. System.out.println(changeIntArrayToString(numArray));
  178. System.out.print("Среднее арифметическое = " + arrayMidSum(numArray));
  179. }
  180.  
  181. private static String changeIntArrayToString(int[] array) {
  182. StringBuilder str = new StringBuilder();
  183. for (int value : array) {
  184. str.append(value);
  185. str.append("; ");
  186. }
  187. return str.toString();
  188. }
  189.  
  190. private static double arrayMidSum(int[] array) {
  191. double sum = 0;
  192. int count = 0;
  193. for (int value : array) {
  194. sum += value;
  195. count++;
  196. }
  197. return sum / count;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement