Advertisement
nikeza

5. Matrix Shuffling

Aug 29th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. package TechFundamentals;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.Scanner;
  7.  
  8. public class array_temp {
  9.  
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         String[] rowCol = scanner.nextLine().split("\\s+");
  15.         int row = Integer.parseInt(rowCol[0]);
  16.         int col = Integer.parseInt(rowCol[1]);
  17.         String[][] matrix = new String[row][col];
  18.         for (int i = 0; i < row; i++) {
  19.             String[] input = scanner.nextLine().split("\\s+");
  20.             matrix[i] = input;
  21.         }
  22.         String command = scanner.nextLine();
  23.         while (!command.equals("END")) {
  24.             String[] tokens = command.split("\\s+");
  25.             if (!isValidInput(tokens)) {
  26.                 System.out.println("Invalid input!");
  27.             } else {
  28.                 if (!checkValidNumber(matrix, tokens)) {
  29.                     System.out.println("Invalid input!");
  30.                 } else {
  31.                     int rowMatrix = Integer.parseInt(tokens[1]);
  32.                     int colMatrix = Integer.parseInt(tokens[2]);
  33.                     int rowChange = Integer.parseInt(tokens[3]);
  34.                     int colChange = Integer.parseInt(tokens[4]);
  35.                     String temp = matrix[rowMatrix][colMatrix];
  36.                     matrix[rowMatrix][colMatrix] = matrix[rowChange][colChange];
  37.                     matrix[rowChange][colChange] = temp;
  38.  
  39.                     printMatrix(matrix, row, col);
  40.                 }
  41.             }
  42.             command = scanner.nextLine();
  43.         }
  44.     }
  45.  
  46.     private static boolean checkValidNumber(String[][] matrix, String[] tokens) {
  47.  
  48.         if (0 > Integer.parseInt(tokens[1]) || Integer.parseInt(tokens[1]) > matrix.length ||
  49.                 0 > Integer.parseInt(tokens[2]) || Integer.parseInt(tokens[2]) > matrix[0].length ||
  50.                 0 > Integer.parseInt(tokens[3]) || Integer.parseInt(tokens[3]) > matrix.length ||
  51.                 0 > Integer.parseInt(tokens[4]) || Integer.parseInt(tokens[4]) > matrix[0].length) {
  52.             return false;
  53.         }
  54.  
  55.         return true;
  56.     }
  57.  
  58.     private static void printMatrix(String[][] matrix, int row, int col) {
  59.         for (int i = 0; i < row; i++) {
  60.             for (int j = 0; j < col; j++) {
  61.                 System.out.print(matrix[i][j] + " ");
  62.             }
  63.             System.out.println();
  64.         }
  65.     }
  66.  
  67.     private static boolean isValidInput(String[] tokens) {
  68.  
  69.         if (!tokens[0].equals("swap") || tokens.length != 5) {
  70.             return false;
  71.         }
  72.         return true;
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement