Advertisement
Eugene0091

Laba_7_1

Apr 8th, 2020
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     public static Scanner scan = new Scanner(System.in);
  12.     public final static String MISTAKE_NOT_FOUND_MESSAGE = "Error! A file with the same name was not found. Try again.";
  13.     public final static String MISTAKE_NOT_OPEN_MESSAGE = "Error! Unable to open this file. Please check the file and try again..\n";
  14.  
  15.     public static void main(String[] args) {
  16.         System.out.println("Данная программа преобразует матрицу инцидентности\n" + "в списки инцидентности.");
  17.         int matrix[][] = readFile();
  18.         System.out.println("Read file:");
  19.         outputInConsole(matrix);
  20.         String[] res = createList(matrix);
  21.         outputResult(res);
  22.         System.out.println("\nProgram completed.");
  23.     }
  24.  
  25.     public static void outputInConsole(int arr[][]) {
  26.         for (int i = 0; i < arr.length; i++) {
  27.             for (int j = 0; j < arr[0].length; j++) {
  28.                 System.out.print(arr[i][j] + "   ");
  29.             }
  30.             System.out.println();
  31.         }
  32.     }
  33.  
  34.     public static String getFileName() {
  35.         final String INPUT_MESSAGE = "Please enter a file name from which data will be read.\nFor example,\"C:\\Users\\Eugene\\Desktop\\Name.txt\".";
  36.         System.out.println(INPUT_MESSAGE);
  37.         String fileName = scan.nextLine();
  38.         return fileName;
  39.     }
  40.  
  41.     public static void outputResult(String[] res) {
  42.         System.out.println("Result:");
  43.             for (int i = 0; i < res.length; i++) {
  44.                 System.out.println((i+1) + ". " + res[i]);
  45.             }
  46.         }
  47.  
  48.     public static int[][] readFile() {
  49.         boolean isInvalidInput;
  50.         int[][] matrix = null;
  51.         do {
  52.             isInvalidInput = false;
  53.             String fileName = getFileName();
  54.             try {
  55.                 BufferedReader input = new BufferedReader(new FileReader(fileName));
  56.                 String text = input.readLine();
  57.                 int row = Integer.parseInt(text);
  58.                 text = input.readLine();
  59.                 int column = Integer.parseInt(text);
  60.                 matrix = new int[row][column];
  61.                 for (int i = 0; i < row; i++) {
  62.                     text = input.readLine();
  63.                     String[] arrFilling = text.split(" ");
  64.                     for (int j = 0; j < column; j++) {
  65.                         matrix[i][j] = Integer.parseInt(arrFilling[j]);
  66.                     }
  67.                 }
  68.                 input.close();
  69.                 isInvalidInput = checkMatrix(matrix);
  70.             } catch (FileNotFoundException e) {
  71.                 System.out.println(MISTAKE_NOT_FOUND_MESSAGE);
  72.                 isInvalidInput = true;
  73.             } catch (IOException e) {
  74.                 System.out.println(MISTAKE_NOT_OPEN_MESSAGE);
  75.                 isInvalidInput = true;
  76.             } catch (NullPointerException e) {
  77.                 System.out.println("Error! Please ckeck file.");
  78.                 isInvalidInput = true;
  79.             } catch (NumberFormatException e) {
  80.                 System.out.println("Error! The file contains invalid data. Please check the file and try again.");
  81.                 isInvalidInput = true;
  82.             }
  83.         } while (isInvalidInput);
  84.         return matrix;
  85.     }
  86.  
  87.     public static boolean checkMatrix(int[][] matrix) {
  88.         int i = 0;
  89.         while (i < matrix.length) {
  90.             int j = 0;
  91.             while (j < matrix[0].length) {
  92.                 if ((matrix[i][j] != 1) & (matrix[i][j] != -1) & (matrix[i][j] != 0)) {
  93.                     System.out.println("Error! В матрице есть элементы не равные {-1, 0, 1}.");
  94.                     return true;
  95.                 }
  96.                 j++;
  97.             }
  98.         i++;
  99.         }
  100.         return false;
  101.     }
  102.  
  103.     public static String[] createList(int[][] matrix) {
  104.         String[] res = new String[matrix.length];
  105.         int count = 0;
  106.         for (int i = 0; i < matrix.length; i++) {
  107.             String s = "";
  108.             for (int j = 0; j < matrix[0].length; j++) {
  109.                 if (matrix[i][j] == 1) {
  110.                     for (int k = 0; k < matrix.length; k++) {
  111.                         if ((matrix[k][j] == 1 || matrix[k][j] == -1) && !(k == i)) {
  112.                             s = s + (k + 1) + " ";
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             res[count] = s;
  118.             count++;
  119.         }
  120.         return res;
  121.     }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement