Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.70 KB | None | 0 0
  1.  
  2. package com.metanit;
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9. import javax.swing.table.DefaultTableModel;
  10. import java.io.*;
  11.  
  12. public class Main {
  13.     static Scanner in = new Scanner(System.in);
  14.  
  15.     public static char GetUserChoice() {
  16.         System.out.println("Do you want to read from file?[Y].If you want to enter with yourself,then enter any other character");
  17.         Scanner sc = new Scanner(System.in);
  18.         char Choice = sc.next().charAt(0);
  19.         return Choice;
  20.     }
  21.  
  22.     public static void InputMatrix(int[][] Matrix) {
  23.         Scanner in = new Scanner(System.in);
  24.         for (int i = 0; i < Matrix.length; i++) {
  25.             for (int j = 0; j < Matrix[i].length; j++) {
  26.                 System.out.print("Введите элемент arr[" + i + "][" + j + "]:");
  27.                 Matrix[i][j] = in.nextInt();
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static void OutputInConsole(int arr[][]) {
  33.         for (int i = 0; i < arr.length; i++) {
  34.             for (int j = 0; j < arr[i].length; j++) {
  35.                 System.out.print(arr[i][j] + "\t");
  36.             }
  37.             System.out.println();
  38.         }
  39.     }
  40.  
  41.     public static void Nol(int[][] Matrix) {
  42.         int counter = 0;
  43.         for (int i = 0; i < Matrix.length; i++) {
  44.             for (int j = 0; j < Matrix[i].length; j++) {
  45.                 if (Matrix[i][j] == 0) {
  46.                     counter++;
  47.                 }
  48.             }
  49.             if (counter > 0) {
  50.                 System.out.println(i + 1);
  51.                 counter = 0;
  52.             }
  53.         }
  54.     }
  55.  
  56.     public static void main(String[] args) throws IOException {
  57.          int M = 5;
  58.          int N = 3;
  59.         char Answer;
  60.         Answer = GetUserChoice();
  61.         int[][] Matrix = new int[M][N];
  62.         if (Answer == 'Y') {
  63.             InputMatrix(Matrix);
  64.             System.out.println("Your array:");
  65.             OutputInConsole(Matrix);
  66.             System.out.println("Rows in which there is zero:");
  67.         } else {
  68.  
  69.             boolean isInvalid = false;
  70.             Scanner in = new Scanner(System.in);
  71.             do {
  72.                 System.out.println("Enter ur txt file location");
  73.                 String location = in.nextLine();
  74.                 File file = new File(location);
  75.                 try {
  76.                     BufferedReader buffer = new BufferedReader(new FileReader(file));
  77.                     String text = buffer.readLine();
  78.                     int high = Integer.parseInt(text);
  79.                     text = buffer.readLine();
  80.                     int wide = Integer.parseInt(text);
  81.                     Matrix = new int[high][wide];
  82.                     for (int i = 0; i < high; i++) {
  83.                         text = buffer.readLine();
  84.                         String[] arrFillings = text.split(" ");
  85.                         for (int j = 0; j <wide; j++)
  86.                             Matrix[i][j]= Integer.parseInt(arrFillings[j]);
  87.                     }
  88.  
  89.                     for (int i = 0; i < high; i++) {
  90.                         text = buffer.readLine();
  91.                         String[] arrFillings = text.split(" ");
  92.                         for (int j = 0; j < wide; j++) {
  93.                             Matrix[i][j] = Integer.parseInt(arrFillings[j]);
  94.                             System.out.println(Matrix[i][j]);
  95.                         }
  96.                     }
  97.                     buffer.close();
  98.                 } catch (FileNotFoundException e) {
  99.                     System.out.println("File not found, try again");
  100.                     isInvalid = true;
  101.                 } catch (IOException e) {
  102.                     System.out.println("This file cant be opened, try again");
  103.                     isInvalid = true;
  104.                 } catch (NumberFormatException e) {
  105.                     System.out.println("Invalid data, try again");
  106.                     isInvalid = true;
  107.                 }
  108.  
  109.             }
  110.             while (isInvalid);
  111.  
  112.         }
  113.  
  114.  
  115.       /*  Scanner in = new Scanner(System.in);
  116.         String fileName;
  117.         System.out.println("Введите имя файла из которого хотите считатинформацию: ");
  118.         fileName = in.nextLine();
  119.         fileName = fileName + ".txt";
  120.         FileInputStream inFile = new FileInputStream(fileName);
  121.         byte[] str = new byte[inFile.available()];
  122.         inFile.read(str);
  123.         String text = new String(str);
  124.         int i, j;
  125.         for (i = 0; i < 2; ++i) {
  126.             for (j = 0; j < 2; ++j) {
  127.                 System.out.println(Matrix[i][j]);
  128.             }
  129.         }*/
  130.         Nol(Matrix);
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement