Advertisement
venik2405

Untitled

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