Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- static Scanner scConsole = new Scanner(System.in);
- private static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static int[][][] getArrayFromConsole(int size) {
- int[][][] arrays = new int[size][size][size];
- boolean isIncorrect;
- System.out.println("Enter the matrix");
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++)
- for (int k = 0; k < size; k++) {
- do {
- isIncorrect = false;
- try {
- System.out.println("Enter the element number [" + (i + 1) + "|" + (j + 1) + "|" + (k + 1) + "]");
- arrays[i][j][k] = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- }
- return arrays;
- }
- public static void printMatrixToConsole(int[][][] matrix, int time) {
- System.out.println("Iteration number [" + time + "]");
- System.out.println("_____________________________");
- for (int lay = 0; lay < matrix.length; lay++) {
- System.out.print("Slay number " + (lay + 1) + ":\n");
- for (int lin = 0; lin < matrix.length; lin++) {
- for (int col = 0; col < matrix.length; col++) {
- System.out.print(matrix[lay][lin][col] + " ");
- }
- System.out.print("\n");
- }
- System.out.println("\n");
- }
- }
- private static int matrixSizeInput() {
- final int MIN_VALUE = 1;
- final int MAX_VALUE = 6;
- boolean isIncorrect;
- int size = 0;
- do {
- isIncorrect = false;
- try {
- size = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if ((!isIncorrect) && ((size < MIN_VALUE) || (size > MAX_VALUE))) {
- System.out.println("Please, enter the natural number less than 6");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- private static void getArrayFromFile(int[][][] arrays) {
- Scanner file = getInputFileLocation();
- for (int i = 0; i < arrays.length; i++) {
- for (int j = 0; j < arrays.length; j++) {
- for (int k = 0; k < arrays.length; k++) {
- arrays[i][j][k] = file.nextInt();
- }
- }
- }
- }
- public static void sortMatrix(int[][][] arrays) {
- int temp;
- int time = 0;
- int up = arrays.length;
- for (int i = 0; i < up; i++) {
- for (int j = 0; j < up; j++) {
- for (int z = 0; z < up; z++) {
- for (int i1 = 0; i1 < up; i1++) {
- for (int j1 = 0; j1 < up; j1++) {
- for (int z1 = 0; z1 < up; z1++) {
- if (arrays[i1][j1][z1] > arrays[i][j][z]) {
- temp = arrays[i1][j1][z1];
- arrays[i1][j1][z1] = arrays[i][j][z];
- arrays[i][j][z] = temp;
- time++;
- printMatrixToConsole(arrays, time);
- }
- }
- }
- }
- }
- }
- }
- }
- private static int chooseInput() {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- private static void outputToFile(int[][][] arrays) {
- PrintWriter out = getOutputFileLocation();
- for (int lay = 0; lay < arrays.length; lay++) {
- out.print("Slay " + (lay + 1) + ":\n");
- for (int lin = 0; lin < arrays.length; lin++) {
- for (int col = 0; col < arrays.length; col++) {
- out.print(arrays[lay][lin][col] + " ");
- }
- out.print("\n");
- }
- out.println("\n");
- }
- out.close();
- }
- public static void main(String[] args) {
- System.out.println("This program sorts a three-dimensional array in ascending order.");
- int chosenInput = chooseInput();
- System.out.println("Enter the matrix dimension");
- int size = matrixSizeInput();
- int[][][] matrixArray = new int[size][size][size];
- if (chosenInput == 0) {
- getArrayFromFile(matrixArray);
- } else {
- matrixArray = getArrayFromConsole(size);
- }
- sortMatrix(matrixArray);
- outputToFile(matrixArray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement