Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. public class LtestTwo {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10.         int rows = Integer.parseInt(read.readLine());
  11.         int cols = Integer.parseInt(read.readLine());
  12.  
  13.         char[][] matrix = readCharMatrix(read, rows); // Work
  14.         char[][] matrix2 = readCharMatrix(read, rows, cols); // Work
  15.  
  16.         //CharTest
  17.  
  18. //        5
  19. //        5
  20. //        a a a a a
  21. //        a a a a a
  22. //        a a a a a
  23. //        a a a a a
  24. //        a a a a a
  25.  
  26.  
  27.         //Char test without row col , because they have already been filed .
  28.  
  29. //        a a a a a
  30. //        a a a a a
  31. //        a a a a a
  32. //        a a a a a
  33. //        a a a a a
  34.  
  35.         printCharMatrix(matrix); //Work
  36.         System.out.println();
  37.         printCharMatrix(matrix2); //Work
  38.  
  39.  
  40.         int[][] intMatrix = readIntMatrix(read); // Work
  41.         printIntMatrix(intMatrix); // Work
  42.  
  43.         // Int test
  44.  
  45. //        5 5
  46. //        1 2 3 4 5
  47. //        6 7 8 9 10
  48. //        11 12 13 14 15
  49. //        16 17 18 19 20
  50. //        21 22 23 24 25
  51.  
  52.  
  53.     }
  54.  
  55.     private static char[][] readCharMatrix(BufferedReader read, int rows) throws IOException { // Work
  56.  
  57.         char[][] matrix = new char[rows][];
  58.  
  59.         for (int row = 0; row < rows; row++) {
  60.             String line = read.readLine();
  61.             String newString = line.replaceAll("\\s+", "");
  62.             matrix[row] = newString.toCharArray();
  63.  
  64.         }
  65.         return matrix;
  66.     }
  67.  
  68.     private static char[][] readCharMatrix(BufferedReader read, int rows, int cols) throws IOException { // Work
  69.  
  70.         char[][] matrix = new char[rows][cols];
  71.  
  72.         for (int row = 0; row < rows; row++) {
  73.             char[] line = read.readLine().replaceAll("\\s+", "").toCharArray();
  74.             for (int col = 0; col < cols; col++) {
  75.                 matrix[row][col] = line[col];
  76.             }
  77.  
  78.         }
  79.         return matrix;
  80.     }
  81.  
  82.     private static void printCharMatrix(char[][] matrix) { // Work
  83.  
  84.         //System.out.println();
  85.  
  86.         for (int row = 0; row < matrix.length; row++) {
  87.             for (int col = 0; col < matrix[row].length; col++) {
  88.                 System.out.print(matrix[row][col] + " ");
  89.             }
  90.             System.out.println();
  91.         }
  92.     }
  93.  
  94.  
  95.     private static int[][] readIntMatrix(BufferedReader read) throws IOException {
  96.  
  97.         String line = read.readLine();
  98.  
  99.         String[] split = line.split("\\s+");
  100.  
  101.         int rows = Integer.parseInt(split[0]);
  102.         int cols = Integer.parseInt(split[1]);
  103.  
  104.         int[][] matrix = new int[rows][cols];
  105.  
  106.         for (int row = 0; row < rows; row++) {
  107.             line = read.readLine();
  108.             split = line.split("\\s+");
  109.  
  110.             for (int col = 0; col < cols; col++) {
  111.                 matrix[row][col] = Integer.parseInt(split[col]);
  112.             }
  113.         }
  114.         return matrix;
  115.     }
  116.  
  117.     private static void printIntMatrix(int[][] matrix) {
  118.  
  119.         for (int row = 0; row < matrix.length; row++) {
  120.  
  121.             for (int col = 0; col < matrix[row].length; col++) {
  122.  
  123.                 System.out.print(matrix[row][col] + " ");
  124.             }
  125.             System.out.println();
  126.         }
  127.     }
  128.  
  129.  
  130.     private static String trimAndDeleteBlanks(BufferedReader read) throws IOException {
  131.         String line = read.readLine().trim();
  132.         while (line.length() == 0) {
  133.             line = read.readLine().trim();
  134.         }
  135.         return line;
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement