Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     private static int [][] matrix;
  9.     private static String Path = "D:\\matrix.txt";
  10.     private static int width = 0;
  11.     private static int height = 0;
  12.  
  13.  
  14.     private static void getMatrix() throws IOException{
  15.  
  16.         try(Scanner sc = new Scanner(new File(Path)))
  17.         {
  18.             int i = 0, j = 0;
  19.             String value;
  20.             while (sc.hasNextLine()) {
  21.                 value = sc.nextLine();
  22.                 if (j == 0) {
  23.                     Scanner sc2 = new Scanner(value);
  24.                     while(sc2.hasNext()){
  25.                         try{
  26.                             sc2.nextInt();
  27.                             j++;
  28.                         }
  29.                         catch (Exception e) {
  30.                             e.printStackTrace();
  31.                         }
  32.                     }
  33.                 }
  34.                 i++;
  35.             }
  36.             width = j;
  37.             height = i;
  38.             System.out.println("Высота:" + i + "  Ширина:" + j);
  39.         }
  40.         catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.  
  44.  
  45.  
  46.  
  47.         matrix = new int [height][width];
  48.  
  49.         try(Scanner sc = new Scanner(new File(Path)))
  50.         {
  51.             int i = 0, j = 0;
  52.             String value;
  53.             while (sc.hasNextLine()) {
  54.                 value = sc.nextLine();
  55.                     Scanner sc2 = new Scanner(value);
  56.                     while(sc2.hasNext()){
  57.                         try{
  58.                             matrix[i][j] = sc2.nextInt();
  59.                             j++;
  60.                         }
  61.                         catch (Exception e) {
  62.                             e.printStackTrace();
  63.                         }
  64.                     }
  65.                 j=0;
  66.                 i++;
  67.             }
  68.         }
  69.         catch (Exception e) {
  70.             e.printStackTrace();
  71.         }
  72.  
  73.         for (int i = 0; i < height; i++) {
  74.             for (int j = 0; j < width; j++) {
  75.                 System.out.print(matrix[i][j] + " ");
  76.             }
  77.             System.out.println();
  78.         }
  79.     }
  80.  
  81.     public static void main(String[] args) throws IOException {
  82.         getMatrix();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement