Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.Scanner;
- public class Graph {
- private static final Scanner scanner = new Scanner(System.in);
- static Scanner fileScanner;
- public static void main(String[] args) {
- int num;
- byte[][] arr;
- System.out.println("Program convert the adjacency matrix into an incident list.");
- System.out.println("Enter type of input." + '\n' + "1 is console input, 0 is file input.");
- boolean chose = choose();
- if (chose) {
- System.out.println("Input size of matrix:");
- num = inputMatrixSize();
- System.out.println("Input matrix elements:");
- arr = inputMatrix(num);
- } else {
- String path = inputFilePath();
- num = inputSizeOfMatrixFromFile(path);
- arr = inputMatrixFile(path, num);
- }
- String result = showIncidentList(arr);
- System.out.println(result);
- }
- private static String showIncidentList (byte[][] arr) {
- String oneVertex = "";
- String result = "";
- boolean isExist = false;
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr.length; j++) {
- if (arr[i][j] == 1) {
- isExist = true;
- oneVertex = oneVertex + (j+ 1) + " ";
- }
- }
- if (isExist) {
- result = result + i + ": " + oneVertex + '\n';
- isExist = false;
- oneVertex = "";
- }
- }
- return result;
- }
- private static boolean choose() {
- int inputNumber = 0;
- boolean isIncorrect;
- final int MIN_NUM = 0;
- final int MAX_NUM = 1;
- do {
- isIncorrect = false;
- try {
- inputNumber = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a number.");
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- System.out.println("You are out of input range!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return inputNumber == 1;
- }
- private static int inputData() {
- int n = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isIncorrect = true;
- System.out.println("Please, enter a integer number:");
- }
- } while (isIncorrect);
- return n;
- }
- private static int inputMatrixElement() {
- boolean isIncorrect;
- int n;
- do {
- n = inputData();
- isIncorrect = false;
- if ( (n != 0) && (n != 1)) {
- isIncorrect = true;
- System.out.println("Only 1 or 0 are valid.");
- }
- } while (isIncorrect);
- return n;
- }
- private static int inputMatrixSize() {
- boolean isIncorrect;
- final int MIN_SIZE = 1;
- final int MAX_SIZE = 10;
- int num;
- do {
- num = inputData();
- isIncorrect = false;
- if ((num < MIN_SIZE) || (num > MAX_SIZE)) {
- System.out.println("Matrix size should be in the range from 1 to 10:");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return num;
- }
- private static byte[][] inputMatrix(int num) {
- byte[][] arr = new byte[num][num];
- for (int i = 0; i < arr.length; i++)
- for (int j = 0; j < arr.length; j++)
- arr[i][j] = (byte) inputMatrixElement();
- if (!checkMatrix(arr)){
- System.out.println("bro, input correct matrix");
- inputMatrix(num);
- }
- return arr;
- }
- private static boolean checkMatrix(byte[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- if (matrix[i][j] != matrix[j][i]) return false;
- }
- }
- return true;
- }
- private static String inputFilePath() {
- String path;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- System.out.println("Input file path:");
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("Wrong way to file.");
- isIncorrect = true;
- }
- if (!file.canRead() && file.exists()) {
- System.out.println("Impossible to read a file.");
- isIncorrect = true;
- }
- if (!file.canWrite() && file.canRead()) {
- System.out.println("Impossible to write a file.");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- private static int inputSizeOfMatrixFromFile(String path) {
- int num = 0;
- boolean isIncorrect;
- final int MIN = 1;
- final int MAX = 10;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- num = fileScanner.nextInt();
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Check file.");
- path = inputFilePath();
- }
- if (!isIncorrect && ((num < MIN) || (num > MAX))) {
- isIncorrect = true;
- System.out.println("Array size should be in the range from 1 to 10:");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- private static boolean checkBinary (int n) {
- return (n == 1) || (n == 0);
- }
- private static byte[][] inputMatrixFile(String path, int num) {
- boolean isIncorrect;
- byte[][] arr = new byte[num][num];
- int n;
- do {
- isIncorrect = false;
- try {
- fileScanner = new Scanner(new File(path));
- fileScanner.nextLine();
- for (int i = 0; (i < arr.length); i++) {
- for (int j = 0; j < arr.length; j++) {
- n = fileScanner.nextInt();
- if (checkBinary(n)) {
- arr[i][j] = (byte) n;
- } else {
- isIncorrect = true;
- System.out.println("Matrix should consist of ones and zeros only!");
- path = inputFilePath();
- }
- }
- }
- if (!checkMatrix(arr)){
- System.out.println("bro, input correct matrix");
- isIncorrect = true;
- path = inputFilePath();
- }
- } catch (Exception q) {
- isIncorrect = true;
- System.out.println("Reading of array elements failed.");
- path = inputFilePath();
- }
- } while (isIncorrect);
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement