Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package laba7_2;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.Writer;
- import java.util.Scanner;
- public class Main {
- private static int[][] matrix;
- private static StringBuilder list;
- private static Scanner scanner = new Scanner(System.in);
- public static void main(String[] args) {
- displayMainMenu();
- }
- private static void displayMainMenu() {
- System.out.println("========");
- System.out.println("LABA 7 2");
- System.out.println("========");
- System.out.println("This program converts adjacent matrix to incident list");
- System.out.println("Choose option by picking a number.");
- System.out.println("1) Input matrix");
- System.out.println("2) Input matrix (file)");
- System.out.println("3) Show list");
- System.out.println("4) Save list");
- System.out.println("5) Exit");
- int picked = 0;
- try {
- picked = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- displayMainMenu();
- return;
- }
- if(picked < 1 || picked > 5) {
- displayMainMenu();
- return;
- }
- switch(picked) {
- case 1:
- inputMatrix();
- return;
- case 2:
- inputMatrixFile();
- return;
- case 3:
- showList();
- return;
- case 4:
- saveList();
- return;
- case 5:
- System.out.println("The program has now terminated.");
- return;
- }
- }
- private static void inputMatrix() {
- int amount = inputFunc("amount of vertices", 2, 6);
- matrix = new int[amount][amount];
- for(int i = 0; i < amount; i++) {
- for(int j = 0; j < amount; j++) {
- int adjacent = inputFunc("adjacent value of vertices " + (i+1) + "-" + (j+1), 0, 10);
- matrix[i][j] = adjacent;
- }
- }
- System.out.println("Matrix successfully added.");
- displayMainMenu();
- }
- private static void inputMatrixFile() {
- System.out.println("Input location of the file:");
- String line;
- try (BufferedReader br = new BufferedReader(new FileReader(new File(scanner.nextLine())))) {
- line = br.readLine();
- int amount = Integer.parseInt(line);
- matrix = new int[amount][amount];
- for(int i = 0; i < amount; i++) {
- line = br.readLine();
- for(int j = 0; j < amount; j++) {
- int adjacent = line.charAt(j) - '0';
- matrix[i][j] = adjacent;
- }
- }
- } catch(Exception e) {
- System.out.println("File was not found.");
- displayMainMenu();
- return;
- }
- System.out.println("Matrix successfully added.");
- displayMainMenu();
- }
- private static void showList() {
- if(matrix == null) {
- System.out.println("Nothing to convert");
- displayMainMenu();
- return;
- }
- convertMatrix();
- System.out.println("\n======LIST======");
- System.out.println(list);
- displayMainMenu();
- }
- private static void saveList() {
- if(matrix == null) {
- System.out.println("Nothing to save");
- displayMainMenu();
- return;
- }
- if(list == null)
- convertMatrix();
- System.out.println("Input location of the file:");
- try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File(scanner.nextLine()), false))) {
- bw.append(list);
- } catch(Exception e) {
- System.out.println("An error has occured");
- displayMainMenu();
- return;
- }
- System.out.println("File successfully saved");
- displayMainMenu();
- }
- private static void convertMatrix() {
- list = new StringBuilder("");
- for(int i = 0; i < matrix.length; i++) {
- list.append((i+1) + ": ");
- for(int j = 0; j < matrix.length; j++) {
- int amount = matrix[i][j];
- for(int k = 0; k < amount; k++) {
- list.append((j+1) + " ");
- }
- }
- list.append("\n");
- }
- }
- private static int inputFunc(String name, int min, int max) {
- boolean incorrect;
- String line;
- int element = 0;
- do {
- incorrect = false;
- System.out.println("Input " + name + ":");
- line = scanner.nextLine();
- try {
- element = Integer.parseInt(line);
- } catch (Exception e) {
- incorrect = true;
- System.out.println("Element should be a numerical value");
- }
- if(element < min || element >= max) {
- incorrect = true;
- System.out.printf("Element should be between %d and %d\n", min, max - 1);
- }
- } while(incorrect);
- return element;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement