Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Matrix {
- public static void main(String[] args) {
- if (args.length != 1) {
- System.err.println("Usage: java Matrix <matrix.txt>");
- System.exit(1);
- }
- String matrix[][] = new String[10][10];
- Scanner scanner = null;
- try {
- scanner = new Scanner(new File(args[0]));
- int lineNr = 0;
- while (scanner.hasNext()) {
- String[] line = scanner.nextLine().split(" ");
- for (int col = 0; col < line.length; col++) {
- matrix[lineNr][col] = line[col];
- }
- lineNr++;
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- assert scanner != null;
- scanner.close();
- }
- printMatrix(matrix);
- }
- private static void printMatrix(String[][] matrix) {
- for (String[] aMatrix : matrix) {
- for (int col = 0; col < aMatrix.length; col++) {
- System.out.print(aMatrix[col]);
- if (col != aMatrix.length - 1) {
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement