Advertisement
venik2405

lab2_2_0

Nov 17th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.47 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class lab2_4 {
  5.  
  6.     static Scanner scConsole = new Scanner(System.in);
  7.  
  8.     private static PrintWriter getOutputFileLocation() {
  9.         boolean isIncorrect;
  10.         String location;
  11.         PrintWriter file = null;
  12.         do {
  13.             isIncorrect = false;
  14.             System.out.println("Enter file location:");
  15.             location = scConsole.nextLine();
  16.             try {
  17.                 file = new PrintWriter(location);
  18.             } catch (FileNotFoundException e) {
  19.                 isIncorrect = true;
  20.                 System.out.println("File with this location is not found");
  21.             }
  22.         } while (isIncorrect);
  23.         if (file != null)
  24.             System.out.println("File opened successfully");
  25.         return file;
  26.     }
  27.  
  28.     private static Scanner getInputFileLocation() {
  29.         boolean isIncorrect;
  30.         String location;
  31.         Scanner file = null;
  32.         do {
  33.             isIncorrect = false;
  34.             System.out.println("Enter file location:");
  35.             location = scConsole.nextLine();
  36.             try {
  37.                 file = new Scanner(new File(location));
  38.             } catch (FileNotFoundException e) {
  39.                 isIncorrect = true;
  40.                 System.out.println("File with this location is not found");
  41.             }
  42.         } while (isIncorrect);
  43.         if (file != null)
  44.             System.out.println("File opened successfully");
  45.         return file;
  46.     }
  47.  
  48.     private static int[][] getArrayFromConsole(int[] size) {
  49.         int i;
  50.         int j;
  51.         int[][] arrays = new int[size[0]][size[0]];
  52.         boolean isIncorrect;
  53.         System.out.println("Enter the matrix");
  54.         for (i = 0; i < size[0]; i++) {
  55.             for (j = 0; j < size[0]; j++)
  56.                 do {
  57.                     isIncorrect = false;
  58.                     try {
  59.                         System.out.println("Enter the element number [" + (i + 1) + "|" + (j + 1) + "]");
  60.                         arrays[i][j] = Integer.parseInt(scConsole.nextLine());
  61.                     } catch (Exception e) {
  62.                         System.out.println("Enter the number");
  63.                         isIncorrect = true;
  64.                     }
  65.                 } while (isIncorrect);
  66.         }
  67.         return arrays;
  68.     }
  69.  
  70.     private static void deleteZeroStrings(int[][] arrays, int[] size) {
  71.         int i = 0;
  72.         int j;
  73.         boolean zeroFounded;
  74.         while (i < size[0]) {
  75.             zeroFounded = false;
  76.             j = 0;
  77.             while ((j < arrays.length) & (!zeroFounded)) {
  78.                 if (arrays[i][j] == 0) {
  79.                     zeroFounded = true;
  80.                     for (int p = i; p < size[0] - 1; p++) {
  81.                         arrays[p] = arrays[p + 1];
  82.                     }
  83.                     size[0]--;
  84.                     i--;
  85.                 }
  86.                 j++;
  87.             }
  88.             i++;
  89.         }
  90.     }
  91.  
  92.     private static void print(int[][] arrays, int[] size) {
  93.         for (int i = 0; i < size[0]; i++) {
  94.             for (int j = 0; j < arrays.length; j++) {
  95.                 System.out.print(arrays[i][j] + " ");
  96.             }
  97.             System.out.println();
  98.         }
  99.     }
  100.  
  101.     private static void resultPrint(int[][] arrays, int[] size) {
  102.         if (size[0] == 0) {
  103.             System.out.println("All strings of this matrix were deleted");
  104.         } else {
  105.             for (int i = 0; i < size[0]; i++) {
  106.                 for (int j = 0; j < arrays.length; j++) {
  107.                     System.out.print(arrays[i][j] + " ");
  108.                 }
  109.                 System.out.println();
  110.             }
  111.         }
  112.     }
  113.  
  114.     private static int[] matrixSizeInput() {
  115.         final int MIN_VALUE = 1;
  116.         final int MAX_VALUE = 6;
  117.         boolean isIncorrect;
  118.         int[] size = new int[1];
  119.         do {
  120.             isIncorrect = false;
  121.             try {
  122.                 size[0] = Integer.parseInt(scConsole.nextLine());
  123.             } catch (Exception e) {
  124.                 System.out.println("Enter the number");
  125.                 isIncorrect = true;
  126.             }
  127.             if ((!isIncorrect) && ((size[0] < MIN_VALUE) || (size[0] > MAX_VALUE))) {
  128.                 System.out.println("Please enter a natural value less than six");
  129.                 isIncorrect = true;
  130.             }
  131.         } while (isIncorrect);
  132.         return size;
  133.     }
  134.  
  135.     private static void getArrayFromFile(int[][] arrays, int[] size) {
  136.         int i;
  137.         int j;
  138.         Scanner file = getInputFileLocation();
  139.         for (i = 0; i < size[0]; i++) {
  140.             for (j = 0; j < size[0]; j++)
  141.                 arrays[i][j] = file.nextInt();
  142.         }
  143.     }
  144.  
  145.     private static int chooseInput() {
  146.         boolean isIncorrect;
  147.         String line;
  148.         do {
  149.             isIncorrect = false;
  150.             System.out.println("Do you want to input from file? (y/n)");
  151.             line = scConsole.nextLine().toLowerCase();
  152.             if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
  153.                 isIncorrect = true;
  154.                 System.out.println("Enter valid answer");
  155.             }
  156.         } while (isIncorrect);
  157.         if (line.equals("y") || line.equals("")) {
  158.             return 0;
  159.         } else {
  160.             return 1;
  161.         }
  162.     }
  163.  
  164.     private static void outputToFile(int[][] arrays, int[] size) {
  165.         PrintWriter out = getOutputFileLocation();
  166.         for (int i = 0; i < size[0]; i++) {
  167.             for (int j = 0; j < arrays.length; j++) {
  168.                 out.print(arrays[i][j]);
  169.             }
  170.             out.println();
  171.         }
  172.         out.close();
  173.     }
  174.  
  175.     public static void main(String[] args) {
  176.         System.out.println("Данная программа удаляет строки массива , содержащие нулевые элементы.");
  177.         int chosenInput = chooseInput();
  178.         System.out.println("Enter the matrix order");
  179.         int[] size = matrixSizeInput();
  180.         int[][] matrixArray = new int[size[0]][size[0]];
  181.         if (chosenInput == 0) {
  182.             getArrayFromFile(matrixArray, size);
  183.         } else {
  184.             matrixArray = getArrayFromConsole(size);
  185.         }
  186.         System.out.println("Original matrix");
  187.         print(matrixArray, size);
  188.         deleteZeroStrings(matrixArray, size);
  189.         System.out.println("Modified matrix");
  190.         resultPrint(matrixArray, size);
  191.         outputToFile(matrixArray, size);
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement