Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.util.Scanner;
- public class Main_2 {
- static int SV = 0; // source vertex
- static int N = 0;
- static int M[][];
- public static void main(String[] args) {
- try {
- int i = 0, j = 0; // counters
- FileInputStream textFile = new FileInputStream("EXAMPLE(2).txt"); // name of input file must go in here
- Scanner scan = new Scanner(textFile);
- N = scan.nextInt(); // read in the size
- String flush = scan.nextLine(); // gets rid of linefeed
- System.out.println(N);
- M = new int[N][N]; // instantiates array
- // this loop reads in matrix from input file
- String line;
- while (i < N && (line = scan.nextLine()) != null) {
- j = 0;
- String delim = " ";
- String tokens[] = line.split(delim);
- for (String a : tokens) {
- M[i][j] = Integer.parseInt(a);
- j++;
- }
- i++;
- }
- if (i > N)
- ;
- SV = scan.nextInt();
- } catch (Exception e) {
- e.printStackTrace();
- }
- printMatrix(M);
- System.out.println(SV);
- }
- public static void printMatrix(int[][] Matrix) {
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N; j++) {
- System.out.print(Matrix[i][j]);
- System.out.print(" ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement