Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public static int[][] readMatrixFromFile() throws FileNotFoundException {
  2.  
  3.  
  4. Scanner input = new Scanner (new File("D:\\Eclipse\\matrix.txt"));
  5. // pre-read in the number of rows/columns
  6. int rows = 0;
  7. int columns = 0;
  8. while(input.hasNextLine())
  9. {
  10. ++rows;
  11. Scanner colReader = new Scanner(input.nextLine());
  12. while(colReader.hasNextInt())
  13. {
  14. ++columns;
  15. }
  16. }
  17. int[][] a = new int[rows][columns];
  18.  
  19. input.close();
  20.  
  21. // read in the data
  22. input = new Scanner(new File("src/array.txt"));
  23. for(int i = 0; i < rows; ++i)
  24. {
  25. for(int j = 0; j < columns; ++j)
  26. {
  27. if(input.hasNextInt())
  28. {
  29. a[i][j] = input.nextInt();
  30. }
  31. }
  32. }
  33. return a;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement