Advertisement
Guest User

Main

a guest
Oct 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileWriter;
  3. import java.util.Scanner;
  4.  
  5. public class Main_2 {
  6. static int SV = 0; // source vertex
  7. static int N = 0;
  8. static int M[][];
  9.  
  10. public static void main(String[] args) {
  11. try {
  12. int i = 0, j = 0; // counters
  13. FileInputStream textFile = new FileInputStream("EXAMPLE(2).txt"); // name of input file must go in here
  14. Scanner scan = new Scanner(textFile);
  15. N = scan.nextInt(); // read in the size
  16. String flush = scan.nextLine(); // gets rid of linefeed
  17. System.out.println(N);
  18. M = new int[N][N]; // instantiates array
  19. // this loop reads in matrix from input file
  20. String line;
  21. while (i < N && (line = scan.nextLine()) != null) {
  22. j = 0;
  23. String delim = " ";
  24. String tokens[] = line.split(delim);
  25. for (String a : tokens) {
  26. M[i][j] = Integer.parseInt(a);
  27. j++;
  28. }
  29. i++;
  30. }
  31. if (i > N)
  32. ;
  33. SV = scan.nextInt();
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37.  
  38. printMatrix(M);
  39. System.out.println(SV);
  40.  
  41. }
  42.  
  43. public static void printMatrix(int[][] Matrix) {
  44. for (int i = 0; i < N; i++) {
  45. for (int j = 0; j < N; j++) {
  46. System.out.print(Matrix[i][j]);
  47. System.out.print(" ");
  48.  
  49. }
  50. System.out.println();
  51. }
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement