Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.metanit;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- import javax.swing.table.DefaultTableModel;
- import java.io.*;
- public class Main {
- static Scanner in = new Scanner(System.in);
- public static char GetUserChoice() {
- System.out.println("Do you want to read from file?[Y].If you want to enter with yourself,then enter any other character");
- Scanner sc = new Scanner(System.in);
- char Choice = sc.next().charAt(0);
- return Choice;
- }
- public static void InputMatrix(int[][] Matrix) {
- Scanner in = new Scanner(System.in);
- for (int i = 0; i < Matrix.length; i++) {
- for (int j = 0; j < Matrix[i].length; j++) {
- System.out.print("Введите элемент arr[" + i + "][" + j + "]:");
- Matrix[i][j] = in.nextInt();
- }
- }
- }
- public static void OutputInConsole(int arr[][]) {
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + "\t");
- }
- System.out.println();
- }
- }
- public static void Nol(int[][] Matrix) {
- int counter = 0;
- for (int i = 0; i < Matrix.length; i++) {
- for (int j = 0; j < Matrix[i].length; j++) {
- if (Matrix[i][j] == 0) {
- counter++;
- }
- }
- if (counter > 0) {
- System.out.println(i + 1);
- counter = 0;
- }
- }
- }
- public static void main(String[] args) throws IOException {
- int M = 5;
- int N = 3;
- char Answer;
- Answer = GetUserChoice();
- int[][] Matrix = new int[M][N];
- if (Answer == 'Y') {
- InputMatrix(Matrix);
- System.out.println("Your array:");
- OutputInConsole(Matrix);
- System.out.println("Rows in which there is zero:");
- } else {
- boolean isInvalid = false;
- Scanner in = new Scanner(System.in);
- do {
- System.out.println("Enter ur txt file location");
- String location = in.nextLine();
- File file = new File(location);
- try {
- BufferedReader buffer = new BufferedReader(new FileReader(file));
- String text = buffer.readLine();
- int high = Integer.parseInt(text);
- text = buffer.readLine();
- int wide = Integer.parseInt(text);
- Matrix = new int[high][wide];
- for (int i = 0; i < high; i++) {
- text = buffer.readLine();
- String[] arrFillings = text.split(" ");
- for (int j = 0; j <wide; j++)
- Matrix[i][j]= Integer.parseInt(arrFillings[j]);
- }
- for (int i = 0; i < high; i++) {
- text = buffer.readLine();
- String[] arrFillings = text.split(" ");
- for (int j = 0; j < wide; j++) {
- Matrix[i][j] = Integer.parseInt(arrFillings[j]);
- System.out.println(Matrix[i][j]);
- }
- }
- buffer.close();
- } catch (FileNotFoundException e) {
- System.out.println("File not found, try again");
- isInvalid = true;
- } catch (IOException e) {
- System.out.println("This file cant be opened, try again");
- isInvalid = true;
- } catch (NumberFormatException e) {
- System.out.println("Invalid data, try again");
- isInvalid = true;
- }
- }
- while (isInvalid);
- }
- /* Scanner in = new Scanner(System.in);
- String fileName;
- System.out.println("Введите имя файла из которого хотите считатинформацию: ");
- fileName = in.nextLine();
- fileName = fileName + ".txt";
- FileInputStream inFile = new FileInputStream(fileName);
- byte[] str = new byte[inFile.available()];
- inFile.read(str);
- String text = new String(str);
- int i, j;
- for (i = 0; i < 2; ++i) {
- for (j = 0; j < 2; ++j) {
- System.out.println(Matrix[i][j]);
- }
- }*/
- Nol(Matrix);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement