Advertisement
vaakata

Ex08_MatrixShufflingV20_21Jan2017

Jan 23rd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3. public class Ex08_MatrixShufflingV20_21Jan2017 {
  4.     public static void main(String[] args){
  5.         Locale.setDefault(Locale.ROOT);
  6.         Scanner scan = new Scanner(System.in);
  7.         String[] matrixSize = scan.nextLine().split("\\s");
  8.         int rows = Integer.parseInt(matrixSize[0]);
  9.         int cols = Integer.parseInt(matrixSize[1]);
  10.  
  11.         String[][] matrix = fillTheMatrix(scan, rows, cols);
  12.         String commands = scan.nextLine();
  13.  
  14.         while(!commands.equals("END")) {
  15.             String[] data = commands.split("\\s");
  16.             if(data.length == 5){
  17.                 String action = data[0];
  18.                 int row1 = Integer.parseInt(data[1]);
  19.                 int col1 = Integer.parseInt(data[2]);
  20.                 int row2 = Integer.parseInt(data[3]);
  21.                 int col2 = Integer.parseInt(data[4]);
  22.                 if(action.equals("swap") && row1 < rows && row2 < rows && col1 < cols && col2 < cols) {
  23.                     matrix = swapItems(data, matrix, rows, cols, row1, col1, row2, col2);
  24.                     for (int row = 0; row < matrix.length; row++) {
  25.                         for (int col = 0; col < matrix[row].length; col++) {
  26.                             System.out.print(matrix[row][col] + " ");
  27.                         }
  28.                         System.out.println();
  29.                     }
  30.                 } else {
  31.                     System.out.println("Invalid input!");
  32.                 }
  33.             } else {
  34.                 System.out.println("Invalid input!");
  35.             }
  36.             commands = scan.nextLine();
  37.         }
  38.     }
  39.  
  40.     private static String[][] swapItems(String[] commands, String[][] matrix, int rows, int cols, int row1, int col1, int row2, int col2) {
  41.         String[][] swapMatrix = matrix;
  42.         String temp = matrix[row1][col1];
  43.         swapMatrix[row1][col1] = matrix[row2][col2];
  44.         swapMatrix[row2][col2] = temp;
  45.         swapMatrix[Integer.parseInt(commands[3])][Integer.parseInt(commands[4])] = temp;
  46.         return swapMatrix;
  47.     }
  48.  
  49.     private static String[][] fillTheMatrix(Scanner scan, int rows, int cols) {
  50.         String[][] inputMatrix = new String[rows][cols];
  51.         for (int row = 0; row < rows; row++) {
  52.             String[] currentRow = scan.nextLine().split("\\s");
  53.  
  54.             for (int col = 0; col < cols; col++) {
  55.                 inputMatrix[row][col] = currentRow[col];
  56.             }
  57.         }
  58.         return inputMatrix;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement