Advertisement
ultiprosan

Reading table-like data with given row/column

Jul 25th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package Test;
  2.  
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6.  
  7. public class Test {
  8.     public static void main(String[] args) {
  9.         String filename = "data.txt";
  10.        
  11.         try {
  12.             FileReader infile = new FileReader(filename);
  13.             Scanner sc = new Scanner(infile);
  14.            
  15.             // Tabel dimensies
  16.             int rij = sc.nextInt();
  17.             System.out.println("Rij: " + rij);
  18.             int kolom = sc.nextInt();
  19.             System.out.println("Kolom: " + kolom);
  20.            
  21.             int[][] table = new int[rij][kolom];
  22.            
  23.             for (int i = 0; i < rij; i++) {
  24.                 for (int j = 0; j < kolom; j++) {
  25.                     table[i][j] = sc.nextInt();
  26.                 }
  27.             }
  28.            
  29.             for (int i = 0; i < rij; i++) {
  30.                 for (int j = 0; j < kolom; j++) {
  31.                     System.out.print(table[i][j] + " ");
  32.                 }
  33.                 System.out.print("\n");
  34.             }
  35.            
  36.             sc.close();
  37.         } catch (IOException iox) {
  38.             System.out.println("Error while reading datafile.");
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement