Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sve;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int rows = Integer.parseInt(scan.nextLine());
- int[][] firstArray = new int[rows][];
- int[][] secondArray = new int[rows][];
- for (int i = 0; i < rows; i++) {
- firstArray[i] = embedInput(scan);
- }
- for (int i = 0; i < rows; i++) {
- secondArray[i] = embedInput(scan);
- }
- int defaultLength = firstArray[0].length + secondArray[0].length;
- boolean matching = true;
- for (int i = 1; i < rows; i++) {
- if (firstArray[i].length + secondArray[i].length != defaultLength) {
- matching = false;
- break;
- }
- }
- if (matching) {
- for (int i = 0; i < rows; i++) {
- StringBuilder reversedSecond = new StringBuilder(Arrays.toString(secondArray[i])).reverse();
- String result = trimAndFix(Arrays.toString(firstArray[i]) + ", " + reversedSecond);
- System.out.println(result);
- }
- }
- else {
- int totalCells = 0;
- for (int i = 0; i < rows; i++) {
- totalCells += firstArray[i].length + secondArray[i].length;
- }
- System.out.println("The total number of cells is: " + totalCells);
- }
- }
- public static String trimAndFix(String str) {
- return "[" + str.replace("], ]", ", ").replace("[", "").replace(" ,", ", ") + "]";
- }
- public static int[] embedInput(Scanner scan) {
- return Arrays.stream(scan.nextLine().trim().split("\\W+"))
- .map(String::trim).mapToInt(Integer::parseInt).toArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment