Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TechFundamentals;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Scanner;
- public class array_temp {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] rowCol = scanner.nextLine().split("\\s+");
- int row = Integer.parseInt(rowCol[0]);
- int col = Integer.parseInt(rowCol[1]);
- String[][] matrix = new String[row][col];
- for (int i = 0; i < row; i++) {
- String[] input = scanner.nextLine().split("\\s+");
- matrix[i] = input;
- }
- String command = scanner.nextLine();
- while (!command.equals("END")) {
- String[] tokens = command.split("\\s+");
- if (!isValidInput(tokens)) {
- System.out.println("Invalid input!");
- } else {
- if (!checkValidNumber(matrix, tokens)) {
- System.out.println("Invalid input!");
- } else {
- int rowMatrix = Integer.parseInt(tokens[1]);
- int colMatrix = Integer.parseInt(tokens[2]);
- int rowChange = Integer.parseInt(tokens[3]);
- int colChange = Integer.parseInt(tokens[4]);
- String temp = matrix[rowMatrix][colMatrix];
- matrix[rowMatrix][colMatrix] = matrix[rowChange][colChange];
- matrix[rowChange][colChange] = temp;
- printMatrix(matrix, row, col);
- }
- }
- command = scanner.nextLine();
- }
- }
- private static boolean checkValidNumber(String[][] matrix, String[] tokens) {
- if (0 > Integer.parseInt(tokens[1]) || Integer.parseInt(tokens[1]) > matrix.length ||
- 0 > Integer.parseInt(tokens[2]) || Integer.parseInt(tokens[2]) > matrix[0].length ||
- 0 > Integer.parseInt(tokens[3]) || Integer.parseInt(tokens[3]) > matrix.length ||
- 0 > Integer.parseInt(tokens[4]) || Integer.parseInt(tokens[4]) > matrix[0].length) {
- return false;
- }
- return true;
- }
- private static void printMatrix(String[][] matrix, int row, int col) {
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- private static boolean isValidInput(String[] tokens) {
- if (!tokens[0].equals("swap") || tokens.length != 5) {
- return false;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement