svetlozar_kirkov

Lego Blocks (New Soludion) [Java]

Sep 27th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package com.sve;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         int rows = Integer.parseInt(scan.nextLine());
  11.         int[][] firstArray = new int[rows][];
  12.         int[][] secondArray = new int[rows][];
  13.         for (int i = 0; i < rows; i++) {
  14.             firstArray[i] = embedInput(scan);
  15.         }
  16.         for (int i = 0; i < rows; i++) {
  17.             secondArray[i] = embedInput(scan);
  18.         }
  19.         int defaultLength = firstArray[0].length + secondArray[0].length;
  20.         boolean matching = true;
  21.         for (int i = 1; i < rows; i++) {
  22.             if (firstArray[i].length + secondArray[i].length != defaultLength) {
  23.                 matching = false;
  24.                 break;
  25.             }
  26.         }
  27.         if (matching) {
  28.             for (int i = 0; i < rows; i++) {
  29.                 StringBuilder reversedSecond = new StringBuilder(Arrays.toString(secondArray[i])).reverse();
  30.                 String result = trimAndFix(Arrays.toString(firstArray[i]) + ", " + reversedSecond);
  31.                 System.out.println(result);
  32.             }
  33.         }
  34.         else {
  35.             int totalCells = 0;
  36.             for (int i = 0; i < rows; i++) {
  37.                 totalCells += firstArray[i].length + secondArray[i].length;
  38.             }
  39.             System.out.println("The total number of cells is: " + totalCells);
  40.         }
  41.     }
  42.  
  43.     public static String trimAndFix(String str) {
  44.         return "[" + str.replace("], ]", ", ").replace("[", "").replace(" ,", ", ") + "]";
  45.     }
  46.  
  47.     public static int[] embedInput(Scanner scan) {
  48.         return Arrays.stream(scan.nextLine().trim().split("\\W+"))
  49.                 .map(String::trim).mapToInt(Integer::parseInt).toArray();
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment