Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static Scanner scan = new Scanner(System.in);
- public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! A file with the same name was not found. Try again.";
- public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to open this file. Please check the file and try again..\n";
- public static void main(String[] args) {
- System.out.println("Данная программа преобразует матрицу инцидентности\n" + "в списки инцидентности.");
- int matrix[][] = readFile();
- System.out.println("Read file:");
- outputInConsole(matrix);
- String[] res = createList(matrix);
- outputResult(res);
- System.out.println("\nProgram completed.");
- }
- public static void outputInConsole(int arr[][]) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[0].length; j++) {
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static String getFileName() {
- final String INPUT_MESSAGE = "Please enter a file name from which data will be read.\nFor example,\"C:\\Users\\Eugene\\Desktop\\Name.txt\".";
- System.out.println(INPUT_MESSAGE);
- String fileName = scan.nextLine();
- return fileName;
- }
- public static void outputResult(String[] res) {
- System.out.println("Result:");
- for (int i = 0; i < res.length; i++) {
- System.out.println((i+1) + ". " + res[i]);
- }
- }
- public static int[][] readFile() {
- boolean isInvalidInput;
- int[][] matrix = null;
- do {
- isInvalidInput = false;
- String fileName = getFileName();
- try {
- BufferedReader input = new BufferedReader(new FileReader(fileName));
- String text = input.readLine();
- int row = Integer.parseInt(text);
- text = input.readLine();
- int column = Integer.parseInt(text);
- matrix = new int[row][column];
- for (int i = 0; i < row; i++) {
- text = input.readLine();
- String[] arrFilling = text.split(" ");
- for (int j = 0; j < column; j++) {
- matrix[i][j] = Integer.parseInt(arrFilling[j]);
- }
- }
- input.close();
- isInvalidInput = checkMatrix(matrix);
- } catch (FileNotFoundException e) {
- System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
- isInvalidInput = true;
- } catch (IOException e) {
- System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
- isInvalidInput = true;
- } catch (NullPointerException e) {
- System.out.println("Error! Please ckeck file.");
- isInvalidInput = true;
- } catch (NumberFormatException e) {
- System.out.println("Error! The file contains invalid data. Please check the file and try again.");
- isInvalidInput = true;
- }
- } while (isInvalidInput);
- return matrix;
- }
- public static boolean checkMatrix(int[][] matrix) {
- int i = 0;
- while (i < matrix.length) {
- int j = 0;
- while (j < matrix[0].length) {
- if ((matrix[i][j] != 1) & (matrix[i][j] != -1) & (matrix[i][j] != 0)) {
- System.out.println("Error! В матрице есть элементы не равные {-1, 0, 1}.");
- return true;
- }
- j++;
- }
- i++;
- }
- return false;
- }
- public static String[] createList(int[][] matrix) {
- String[] res = new String[matrix.length];
- int count = 0;
- for (int i = 0; i < matrix.length; i++) {
- String s = "";
- for (int j = 0; j < matrix[0].length; j++) {
- if (matrix[i][j] == 1) {
- for (int k = 0; k < matrix.length; k++) {
- if ((matrix[k][j] == 1 || matrix[k][j] == -1) && !(k == i)) {
- s = s + (k + 1) + " ";
- }
- }
- }
- }
- res[count] = s;
- count++;
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement