Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // 2020/4/24(金)~25(土)
  2.  
  3. import java.io.*;
  4.  
  5. public class TestFileInput {
  6. public static void main(String[] args) throws Exception {
  7. System.out.println("\n行列を入力します");
  8. double[][] matrix;
  9. if (args.length == 0) {
  10. System.out.println("ファイル名の指定がありません");
  11. return;
  12. } else if (args[0].equals("-i") && args.length == 2) {
  13. matrix = readFile(args[1]);
  14. } else {
  15. return;
  16. }
  17. }
  18.  
  19. public static double[][] readFile(String fileName) throws Exception {
  20. FileReader fr = new FileReader(fileName);
  21. BufferedReader br = new BufferedReader(fr);
  22. String line = null;
  23. int lineCount = 0;
  24. String[] lineArray = null;
  25. while((line = br.readLine()) != null) {
  26. if (lineCount == 0) {
  27. lineArray = line.split("[ ]");
  28. }
  29. lineCount++;
  30. }
  31. br.close();
  32. double[][] matrix = new double[lineArray.length][lineCount];
  33.  
  34. fr = new FileReader(fileName);
  35. br = new BufferedReader(fr);
  36. line = null;
  37. int lineNum = 0;
  38. String[][] lineTable = new String[lineArray.length][lineCount];
  39. while((line = br.readLine()) != null) {
  40. lineTable[lineNum] = line.split("[ ]");
  41. lineNum++;
  42. }
  43. for (int i = 0; i < lineArray.length; i++) {
  44. for (int j = 0; j < lineCount; j++) {
  45. matrix[i][j] = Double.parseDouble(lineTable[i][j]);
  46. System.out.print(matrix[i][j] + " ");
  47. }
  48. System.out.println();
  49. }
  50. br.close();
  51. return matrix;
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement