Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class lab2_5 {
- 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][size];
- boolean isIncorrect;
- System.out.println("Enter the matrix");
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++)
- do {
- isIncorrect = false;
- try {
- 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) {
- zeroFounded = false;
- j = 0;
- while ((j < arrays.length) & (!zeroFounded)) {
- if (arrays[i][j] == 0) {
- zeroFounded = true;
- for (int p = i; p < size - 1; p++) {
- arrays[p] = arrays[p + 1];
- }
- size--;
- i--;
- }
- j++;
- }
- i++;
- }
- }
- private static void print(int[][] arrays, int size) {
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- System.out.print(arrays[i][j] + " ");
- }
- System.out.println();
- }
- }
- private static void resultPrint(int[][] arrays, int size) {
- for (int i = 0; i < size; 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 = 0;
- do {
- isIncorrect = false;
- try {
- size = Integer.parseInt(scConsole.nextLine());
- } catch (Exception e) {
- System.out.println("Enter the number");
- isIncorrect = true;
- }
- if ((!isIncorrect) && ((size < MIN_VALUE) || (size > 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; i++) {
- for (j = i; j < size; 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(double[] resultArray, int size) {
- PrintWriter out = getOutputFileLocation();
- for (int i = 0; i < size; i++) {
- out.print("x" + (i + 1) + " = " + resultArray[i]);
- out.print('\n');
- }
- out.close();
- }
- public static void main(String[] args) {
- System.out.println("Данная программа удаляет строки массива , содержащие нулевые элементы.");
- int chosenInput = chooseInput();
- System.out.println("Enter the matrix order");
- int size = matrixSizeInput();
- int[][] matrixArray = new int[size][size];
- if (chosenInput == 0) {
- getArrayFromFile(matrixArray, size);
- } else {
- matrixArray = getArrayFromConsole(size);
- }
- double[] resultArray = new double[size];
- System.out.println("Original matrix");
- print(matrixArray, size);
- deleteZeroStrings(matrixArray, size);
- System.out.println("Modified matrix");
- resultPrint(matrixArray, size);
- outputToFile(resultArray, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement