Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int row=Integer.parseInt(scanner.nextLine());
- String[][] matrix=new String[row][];
- for (int i = 0; i < row; i++) {
- String line=scanner.nextLine();
- matrix[i]=line.split(", ");
- }
- String[] command=scanner.nextLine().split(" ");
- if("hide".equals(command[0])) {
- hideColumnAndPrint(matrix,command[1]);
- } else if("sort".equals(command[0])) {
- sortByColumnAndPrint(matrix,command[1]);
- } else if("filter".equals(command[0])) {
- filterByColumnAndPrint(matrix,command[1],command[2]);
- }
- }
- private static void filterByColumnAndPrint(String[][] matrix, String filterColumn, String filterValue) {
- int filterColumnIndex=getColumnIndexByName(matrix[0],filterColumn);
- System.out.println(String.join(" | ",matrix[0]));
- for (int r = 1; r < matrix.length; r++) {
- if(matrix[r][filterColumnIndex].equals(filterValue)) {
- System.out.println(String.join(" | ",matrix[r]));
- }
- }
- }
- private static void sortByColumnAndPrint(String[][] matrix, String sortColumn) {
- int sortColumnIndex=getColumnIndexByName(matrix[0],sortColumn);
- for (int i = 0; i < matrix.length; i++) {
- for (int r = 1; r < matrix.length-1-i; r++) {
- if(matrix[r][sortColumnIndex]
- .compareTo(matrix[r+1][sortColumnIndex])>0) {
- String[] temp=matrix[r];
- matrix[r]=matrix[r+1];
- matrix[r+1]=temp;
- }
- }
- }
- for (int r = 0; r < matrix.length; r++) {
- for (int c = 0; c < matrix[r].length; c++) {
- System.out.print(matrix[r][c]);
- if(c!=matrix[r].length-1) {
- System.out.print(" | ");
- }
- }
- System.out.println();
- }
- }
- private static void hideColumnAndPrint(String[][] matrix, String hiddenColumn) {
- int hiddenColumnIndex=getColumnIndexByName(matrix[0], hiddenColumn);
- for (int r = 0; r < matrix.length; r++) {
- for (int c = 0; c < matrix[r].length; c++) {
- if(c==hiddenColumnIndex){
- continue;
- }
- System.out.print(matrix[r][c]);
- if(c!=matrix[r].length-1) {
- if (!(c == matrix[r].length - 2 && hiddenColumnIndex == c + 1)) {
- System.out.print(" | ");
- }
- }
- }
- System.out.println();
- }
- }
- private static int getColumnIndexByName(String[] matrix, String columnName) {
- int hiddenColumnIndex = -1;
- for (int r = 0; r < matrix.length; r++) {
- if(matrix[r].equals(columnName)) {
- hiddenColumnIndex=r;
- break;
- }
- }
- return hiddenColumnIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement