Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- final static int MIN_N = 2;
- final static int MAX_N = 100;
- final static int MIN_A = -10000;
- final static int MAX_A = 10000;
- static Scanner in = new Scanner(System.in);
- public static int inputAndCheckChoice(boolean isIncorrect) {
- isIncorrect = true;
- int choice = 0;
- do {
- System.out.println("Введите ваш выбор.");
- choice = Integer.parseInt(in.nextLine());
- if (choice != 1 && choice != 2) {
- System.err.println("Введите 1 или 2.");
- } else {
- isIncorrect = false;
- }
- } while (isIncorrect);
- return choice;
- }
- public static int inputAndCheckN(boolean isIncorrect) {
- int n = 0;
- do {
- isIncorrect = false;
- System.out.println("Введите порядок матрицы n: ");
- try {
- n = Integer.parseInt(in.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- System.err.println("Некорректный ввод. Введите целое число.");
- }
- if (!isIncorrect && (n < MIN_N || n > MAX_N)) {
- isIncorrect = true;
- System.err.println("Диапазон значений n от " + MIN_N + " до " + MAX_N + ".");
- }
- } while (isIncorrect);
- return n;
- }
- public static boolean isEof(FileTypes fileType) {
- boolean isEof = false;
- File tempFile;
- try (BufferedReader br = new BufferedReader(new BufferedReader(fileType))
- ) {
- String line;
- if ((line = br.readLine()) != null) {
- isEof = true;
- }
- } catch (IOException e) {
- System.err.println("Файл пустой.");
- }
- return isEof;
- }
- public static String inputFileName(FileTypes fileType, boolean isIncorrect) {
- String fileName = "";
- isIncorrect = true;
- File tempFile;
- do {
- System.out.println("Введите имя файла с путём.");
- fileName = in.nextLine();
- tempFile = new File(fileName);
- if (!tempFile.exists()) {
- System.out.println("Файл не существует.");
- } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
- System.out.println("Файл не является текстовым.");
- } else if (fileType == FileTypes.IN && !tempFile.canRead()) {
- System.out.println("Этот файл невозможно открыть для чтения.");
- } else if (fileType == FileTypes.IN && !isEof()) {
- System.out.println("Этот файл пустой.");
- } else if (fileType == FileTypes.OUT && !tempFile.canWrite()) {
- System.out.println("Этот файл невозможно открыть для записи.");
- } else {
- isIncorrect = false;
- }
- }
- return isIncorrect;
- }
- public static String requestFileNameForReading(boolean isIncorrect) {
- String fileName = "";
- isIncorrect = true;
- File tempFile;
- do {
- System.out.println("Введите имя файла с путём.");
- fileName = in.nextLine();
- tempFile = new File(fileName);
- if (!tempFile.exists()) {
- System.out.println("Файл не существует.");
- } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
- System.out.println("Файл не является текстовым.");
- } else if (!tempFile.canRead()) {
- System.out.println("Этот файл невозможно открыть для чтения.");
- } else if (!isEof()) {
- System.out.println("Этот файл пустой.");
- } else {
- isIncorrect = false;
- }
- } while isIncorrect;
- return fileName;
- }
- public static String requestFileNameForWriting(boolean isIncorrect) {
- String fileName = "";
- isIncorrect = true;
- File tempFile;
- do {
- System.out.println("Введите имя файла с путём.");
- fileName = in.nextLine();
- tempFile = new File(fileName);
- if (!tempFile.exists()) {
- System.out.println("Файл не существует.");
- } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
- System.out.println("Файл не является текстовым.");
- } else if (!tempFile.canWrite()) {
- System.out.println("Этот файл невозможно открыть для записи.");
- } else {
- isIncorrect = false;
- }
- } while isIncorrect;
- return fileName;
- }
- public static int readSizeFromFile(final String fileName) {
- int size = 0;
- int count = 0;
- double temp;
- String[] doubleInString = "";
- String templine;
- try (BufferedReader fReader = new BufferedReader(new FileReader(fileName))) {
- while ((templine = fReader.readLine()) != null) {
- doubleInString = fReader.readLine().split(" ");
- count += doubleInString.length;
- }
- } catch (IOException e) {
- System.err.println("Непредвиденная ошибка.");
- }
- }
- public static double[] readMatrix (final int size; final String fileName) {
- double[][] matrix = new double[size][size];
- String[] doubleInString = "";
- try (BufferedReader fReader = new BufferedReader(new FileReader(fileName))) {
- for (int i = 0; i < size; i++) {
- doubleInString = fReader.readLine().split(" ");
- for (int j = 0; j < size; j++) {
- matrix[i][j] = Double.parseDouble(doubleInString[j]);
- }
- }
- } catch (NumberFormatException e) {
- System.err.println("Некорректный тип данных.");
- matrix = null;
- } catch (IOException e) {
- System.err.println("Непредвиденная ошибка.");
- matrix = null;
- }
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (matrix != null && (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A)) {
- System.err.println("Ошибка при считывании матрицы из файла.");
- }
- }
- }
- return matrix;
- }
- public static void createMatrix(final int n; double[][] matrix;
- boolean isIncorrect)
- {
- for (int i = 0; i < n; i++) {
- for (int j = 0:j < n;
- j++){
- do {
- isIncorrect = false;
- System.out.println("Введите элемент матрицы А" + i + j + ": ");
- try {
- matrix[i][j] = Double.parseDouble(in.nextLine());
- } catch (NumberFormatException e) {
- isIncorrect = true;
- System.err.println("Некорректный ввод. Введите число.");
- }
- if (!isIncorrect && (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A)) {
- isIncorrect = true;
- System.err.println("Диапазон значений A от " + MIN_A + " до " + MAX_A + ".");
- }
- } while (isIncorrect);
- }
- }
- }
- public static double sumElements(double[][] matrix; int n )
- {
- int sum = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- sum += matrix[i][j];
- }
- }
- return sum;
- }
- public static boolean writeToFile(final String fileName; final double sum; boolean isIncorrect) {
- isIncorrect = true;
- try (BufferedWriter fWriter = new BufferedWriter(new FileWriter(fileName))) {
- fWriter.write(sum);
- System.out.println("Результат выведен в файле.");
- isIncorrect = false;
- }
- catch (IOException e) {
- System.err.println("Непредвиденная ошибка.");
- }
- return isIncorrect;
- }
- public static void main(String[] args) {
- int choice;
- int n;
- boolean isIncorrect;
- System.out.println("Откуда Вы хотите вводить данные? \n 1 - консоль, 2 - файл.");
- choice = inputAndCheckChoice(isIncorrect);
- n = inputAndCheckN(isIncorrect);
- double[][] matrix = new double[n][n];
- if (choice == 1) {
- createMatrix(n, matrix, isIncorrect);
- } else {
- do {
- fileName = requestFileNameForReading(isIncorrect);
- matrix = readMatrix(size, fileName);
- } while isIncorrect;
- }
- sum = sumElements(matrix, n);
- System.out.println("Куда Вы хотите вывести результат? \n 1 - консоль, 2 - файл.");
- choice = inputAndCheckChoice();
- if (choice == 1) {
- System.out.println("Сумма равна " + sum);
- } else {
- do {
- fileName = requestFileNameForWriting(isIncorrect);
- isIncorrect = writeToFile();
- } while isIncorrect;
- }
- in.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment