Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.Scanner;
- import static java.lang.Integer.parseInt;
- public class Class {
- public static void main(String[] args) throws IOException {
- int[] numArray = new int[0];
- numArray = programInput(numArray);
- Class.programOutput(numArray);
- }
- private static int[] programInput(int[] numArray) throws IOException {
- boolean isInputCorrect = false;
- while (!isInputCorrect) {
- boolean isInputMethod = isMakeChoice();
- if (isInputMethod) {
- numArray = consoleInput();
- isInputCorrect = true;
- } else {
- numArray = fileInput();
- isInputCorrect = true;
- }
- }
- return numArray;
- }
- private static void programOutput(int[] numArray) throws IOException {
- boolean outputCorrect = false;
- while (!outputCorrect) {
- boolean outputChoice = isMakeChoice();
- if (outputChoice) {
- programAnswerShow(numArray);
- outputCorrect = true;
- } else {
- fileOutput(numArray);
- outputCorrect = true;
- }
- }
- }
- private static void fileOutput(int[] numArray) throws IOException {
- Scanner in = new Scanner(System.in);
- System.out.println("Введите путь к файлу:");
- boolean correct = false;
- do {
- String filePath = in.nextLine();
- try {
- FileWriter output = new FileWriter(filePath);
- output.write("Ваш массив: " + changeIntArrayToString(numArray));
- output.write("\n" + "Среднее арифметическое: " + arrayMidSum(numArray));
- output.close();
- } catch (FileNotFoundException fnf) {
- System.out.println("Не удается найти указанный файл, повторите попытку:");
- correct = true;
- }
- } while (correct);
- }
- private static boolean isMakeChoice() {
- System.out.println("Введите 1 для ввода с консоли или 2, чтобы использовать файл. ");
- Scanner in = new Scanner(System.in);
- boolean correct;
- String choiceNum;
- do {
- choiceNum = in.nextLine();
- correct = !choiceNum.equalsIgnoreCase("1") && !choiceNum.equalsIgnoreCase("2");
- if (correct) {
- System.out.println("Вы можете ввести только 1 или 2, повторите попытку:");
- }
- } while (correct);
- boolean choice = false;
- if (choiceNum.equalsIgnoreCase("1")) {
- choice = true;
- }
- return choice;
- }
- private static boolean isIncorrectInput(String strArray) {
- boolean result = false;
- if (strArray.charAt(0) != ' ' && !strArray.contains(",,") && strArray.charAt(0) != ',') {
- strArray = commaDelete(strArray);
- strArray = spaceDelete(strArray);
- try {
- parseInt(strArray);
- } catch (NumberFormatException numF) {
- result = true;
- }
- }
- return result;
- }
- private static String spaceDelete(String strArray) {
- StringBuilder newStrArray = new StringBuilder();
- for (int i = 0; i < strArray.length(); i++) {
- if (strArray.charAt(i) != ' ') {
- newStrArray.append(strArray.charAt(i));
- }
- }
- return newStrArray.toString();
- }
- private static String commaDelete(String strArray) {
- StringBuilder newStrArray = new StringBuilder();
- for (int i = 0; i < strArray.length(); i++) {
- if (strArray.charAt(i) != ',') {
- newStrArray.append(strArray.charAt(i));
- }
- }
- return newStrArray.toString();
- }
- private static int[] consoleInput() {
- System.out.println("Введите элементы массива через запятую:");
- Scanner arrayIn = new Scanner(System.in);
- String strArray;
- boolean correct;
- do {
- strArray = arrayIn.nextLine();
- correct = isIncorrectInput(strArray);
- if (correct) {
- System.out.println("Перепроверьте введенные данные и повторите попытку:");
- correct = true;
- }
- } while (correct);
- strArray = spaceDelete(strArray);
- return changeStringToIntArray(strArray);
- }
- private static int[] fileInput() throws IOException {
- Scanner arrayIn = new Scanner(System.in);
- System.out.println("Введите путь к файлу:");
- String strArray = "";
- boolean correct;
- do {
- String filePath = arrayIn.nextLine();
- try {
- BufferedReader in = new BufferedReader(new FileReader(filePath));
- strArray = in.readLine();
- in.close();
- correct = isIncorrectInput(strArray);
- if (correct) {
- System.out.println("Некорректные данные файла, исправьте ошибку и повторите попытку:");
- correct = true;
- }
- } catch (FileNotFoundException e) {
- System.out.println("Перепроверьте введенный путь и повторите попытку:");
- correct = true;
- } catch (NullPointerException nullP) {
- System.out.println("Вы ввели пустой файл, проверьте введенные данные и повторите попытку:");
- correct = true;
- }
- } while (correct || isIncorrectInput(strArray));
- strArray = spaceDelete(strArray);
- return changeStringToIntArray(strArray);
- }
- private static int[] changeStringToIntArray(String strArray) {
- String[] array = strArray.split(",");
- int[] numArray = new int[array.length];
- for (int i = 0; i < array.length; i++) {
- numArray[i] = parseInt(array[i]);
- }
- return numArray;
- }
- private static void programAnswerShow(int[] numArray) {
- System.out.print("Ваш массив: ");
- System.out.println(changeIntArrayToString(numArray));
- System.out.print("Среднее арифметическое = " + arrayMidSum(numArray));
- }
- private static String changeIntArrayToString(int[] array) {
- StringBuilder str = new StringBuilder();
- for (int value : array) {
- str.append(value);
- str.append("; ");
- }
- return str.toString();
- }
- private static double arrayMidSum(int[] array) {
- double sum = 0;
- int count = 0;
- for (int value : array) {
- sum += value;
- count++;
- }
- return sum / count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement