Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- class Const{
- public static int size;
- }
- public class lab2_4 {
- static Scanner scConsole = new Scanner(System.in);
- private static PrintWriter getOutputFileLocation() {
- boolean isIncorrect;
- String location;
- PrintWriter file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new PrintWriter(location);
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static Scanner getInputFileLocation() {
- boolean isIncorrect;
- String location;
- Scanner file = null;
- do {
- isIncorrect = false;
- System.out.println("Enter file location:");
- location = scConsole.nextLine();
- try {
- file = new Scanner(new File(location));
- } catch (FileNotFoundException e) {
- isIncorrect = true;
- System.out.println("File with this location is not found");
- }
- } while (isIncorrect);
- if (file != null)
- System.out.println("File opened successfully");
- return file;
- }
- private static int[][] getArrayFromConsole(int[] size) {
- int i;
- int j;
- int[][] arrays = new int[size[0]][size[0]];
- boolean isIncorrect;
- System.out.println("Enter the matrix");
- for (i = 0; i < size[0]; i++) {
- for (j = 0; j < size[0]; j++)
- do {
- isIncorrect = false;
- try {
- System.out.println("Enter the element number [" + (i + 1) + "|" + (j + 1) + "]");
- arrays[i][j] = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- return arrays;
- }
- private static void deleteZeroStrings(int[][] arrays, int[] size) {
- int i = 0;
- int j;
- boolean zeroFounded;
- while (i < size[0]) {
- zeroFounded = false;
- j = 0;
- while ((j < arrays.length) & (!zeroFounded)) {
- if (arrays[i][j] == 0) {
- zeroFounded = true;
- for (int p = i; p < size[0] - 1; p++) {
- arrays[p] = arrays[p + 1];
- }
- size[0]--;
- i--;
- }
- j++;
- }
- i++;
- }
- }
- private static void print(int[][] arrays, int[] size) {
- for (int i = 0; i < size[0]; i++) {
- for (int j = 0; j < arrays.length; j++) {
- System.out.print(arrays[i][j] + " ");
- }
- System.out.println();
- }
- }
- private static void resultPrint(int[][] arrays, int[] size) {
- if (size[0] == 0) {
- System.out.println("All strings of this matrix were deleted");
- } else {
- for (int i = 0; i < size[0]; i++) {
- for (int j = 0; j < arrays.length; j++) {
- System.out.print(arrays[i][j] + " ");
- }
- System.out.println();
- }
- }
- }
- private static int[] matrixSizeInput() {
- final int MIN_VALUE = 1;
- final int MAX_VALUE = 6;
- boolean isIncorrect;
- int[] size = new int[1];
- do {
- isIncorrect = false;
- try {
- size[0] = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if ((!isIncorrect) && ((size[0] < MIN_VALUE) || (size[0] > MAX_VALUE))) {
- System.out.println("Please enter a natural value less than six");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return size;
- }
- private static void getArrayFromFile(int[][] arrays, int[] size) {
- int i;
- int j;
- Scanner file = getInputFileLocation();
- for (i = 0; i < size[0]; i++) {
- for (j = 0; j < size[0]; j++)
- arrays[i][j] = file.nextInt();
- }
- }
- private static int chooseInput() {
- boolean isIncorrect;
- String line;
- do {
- isIncorrect = false;
- System.out.println("Do you want to input from file? (y/n)");
- line = scConsole.nextLine().toLowerCase();
- if (!line.equals("y") && !line.equals("n") && !line.equals("")) {
- isIncorrect = true;
- System.out.println("Enter valid answer");
- }
- } while (isIncorrect);
- if (line.equals("y") || line.equals("")) {
- return 0;
- } else {
- return 1;
- }
- }
- private static void outputToFile(int[][] arrays, int[] size) {
- PrintWriter out = getOutputFileLocation();
- for (int i = 0; i < size[0]; i++) {
- for (int j = 0; j < arrays.length; j++) {
- out.print(arrays[i][j]);
- }
- out.println();
- }
- out.close();
- }
- public static void main(String[] args) {
- System.out.println("Данная программа удаляет строки массива , содержащие нулевые элементы.");
- int chosenInput = chooseInput();
- System.out.println("Enter the matrix order");
- Const.size = matrixSizeInput();
- int[][] matrixArray = new int[size[0]][size[0]];
- if (chosenInput == 0) {
- getArrayFromFile(matrixArray, size);
- } else {
- matrixArray = getArrayFromConsole(size);
- }
- System.out.println("Original matrix");
- print(matrixArray, size);
- deleteZeroStrings(matrixArray, size);
- System.out.println("Modified matrix");
- resultPrint(matrixArray, size);
- outputToFile(matrixArray, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement