Advertisement
venik2405

lab2_2_1

Nov 13th, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class lab2_5 {
  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][size];
  52.         boolean isIncorrect;
  53.  
  54.         System.out.println("Enter the matrix");
  55.         for (i = 0; i < size; i++) {
  56.             for (j = 0; j < size; j++)
  57.                 do {
  58.                     isIncorrect = false;
  59.                     try {
  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) {
  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 - 1; p++) {
  81.                         arrays[p] = arrays[p + 1];
  82.                     }
  83.                     size--;
  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; i++) {
  94.             for (int j = 0; j < size; 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.         for (int i = 0; i < size; i++) {
  103.             for (int j = 0; j < arrays.length; j++) {
  104.                 System.out.print(arrays[i][j] + " ");
  105.             }
  106.             System.out.println();
  107.         }
  108.     }
  109.  
  110.     private static int matrixSizeInput() {
  111.         final int MIN_VALUE = 1;
  112.         final int MAX_VALUE = 6;
  113.         boolean isIncorrect;
  114.         int size = 0;
  115.         do {
  116.             isIncorrect = false;
  117.             try {
  118.                 size = Integer.parseInt(scConsole.nextLine());
  119.             } catch (Exception e) {
  120.                 System.out.println("Enter the number");
  121.                 isIncorrect = true;
  122.             }
  123.             if ((!isIncorrect) && ((size < MIN_VALUE) || (size > MAX_VALUE))) {
  124.                 System.out.println("Please enter a natural value less than six");
  125.                 isIncorrect = true;
  126.             }
  127.         } while (isIncorrect);
  128.         return size;
  129.     }
  130.  
  131.     private static void getArrayFromFile(int[][] arrays, int size) {
  132.         int i;
  133.         int j;
  134.         Scanner file = getInputFileLocation();
  135.         for (i = 0; i < size; i++) {
  136.             for (j = i; j < size; j++)
  137.                 arrays[i][j] = file.nextInt();
  138.         }
  139.     }
  140.  
  141.     private static int chooseInput() {
  142.         boolean isIncorrect;
  143.         String line;
  144.         do {
  145.             isIncorrect = false;
  146.             System.out.println("Do you want to input from file? (y/n)");
  147.             line = scConsole.nextLine().toLowerCase();
  148.             if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
  149.                 isIncorrect = true;
  150.                 System.out.println("Enter valid answer");
  151.             }
  152.         } while (isIncorrect);
  153.         if (line.equals("y") || line.equals(""))
  154.             return 0;
  155.         else
  156.             return 1;
  157.     }
  158.  
  159.     private static void outputToFile(double[] resultArray, int size) {
  160.  
  161.         PrintWriter out = getOutputFileLocation();
  162.  
  163.         for (int i = 0; i < size; i++) {
  164.             out.print("x" + (i + 1) + " = " + resultArray[i]);
  165.             out.print('\n');
  166.         }
  167.         out.close();
  168.     }
  169.  
  170.     public static void main(String[] args) {
  171.         System.out.println("Данная программа удаляет строки массива , содержащие нулевые элементы.");
  172.         int chosenInput = chooseInput();
  173.         System.out.println("Enter the matrix order");
  174.         int size = matrixSizeInput();
  175.         int[][] matrixArray = new int[size][size];
  176.         if (chosenInput == 0) {
  177.             getArrayFromFile(matrixArray, size);
  178.         } else {
  179.             matrixArray = getArrayFromConsole(size);
  180.         }
  181.         double[] resultArray = new double[size];
  182.         System.out.println("Original matrix");
  183.         print(matrixArray, size);
  184.         deleteZeroStrings(matrixArray, size);
  185.         System.out.println("Modified matrix");
  186.         resultPrint(matrixArray, size);
  187.         outputToFile(resultArray, size);
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement