Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- private static int [][] matrix;
- private static String Path = "D:\\matrix.txt";
- private static int width = 0;
- private static int height = 0;
- private static void getMatrix() throws IOException{
- try(Scanner sc = new Scanner(new File(Path)))
- {
- int i = 0, j = 0;
- String value;
- while (sc.hasNextLine()) {
- value = sc.nextLine();
- if (j == 0) {
- Scanner sc2 = new Scanner(value);
- while(sc2.hasNext()){
- try{
- sc2.nextInt();
- j++;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- i++;
- }
- width = j;
- height = i;
- System.out.println("Высота:" + i + " Ширина:" + j);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- matrix = new int [height][width];
- try(Scanner sc = new Scanner(new File(Path)))
- {
- int i = 0, j = 0;
- String value;
- while (sc.hasNextLine()) {
- value = sc.nextLine();
- Scanner sc2 = new Scanner(value);
- while(sc2.hasNext()){
- try{
- matrix[i][j] = sc2.nextInt();
- j++;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- j=0;
- i++;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- }
- public static void main(String[] args) throws IOException {
- getMatrix();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement