fork download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4. class Main {
  5. final static String filename = "renritsu.txt";
  6. public static void main(String args[]){
  7. System.out.println("start.");
  8. double[][] A = null;
  9.  
  10. //read from data
  11. try {
  12. BufferedReader br = new BufferedReader(new FileReader(filename));
  13. String s;
  14. String[] vals;
  15. int rows = 0, cols = 0;
  16. while((s = br.readLine()) != null){
  17. vals = s.split(" ");
  18. cols = Math.max(cols, vals.length);
  19. rows++;
  20. }
  21. System.out.println(rows+"×"+cols+" array found");
  22. A = new double[rows][cols];
  23. br.close();
  24.  
  25. br = new BufferedReader(new FileReader(filename));
  26. rows = cols = 0;
  27. while((s = br.readLine()) !=null){
  28. vals =s.split(" ");//split the line with space
  29. cols = vals.length;
  30. for(int i=0; i<cols;i++)
  31. A[rows][i]=Double.parseDouble(vals[i]);
  32. rows++;
  33. }
  34. } catch (IOException e){
  35. System.out.println(e);
  36. }
  37. dump(A);
  38.  
  39. //forward
  40. for(int i = 0; i< A.length ;i++) {
  41. double P = A[i][i];//pivot
  42. for(int j = 0; j < A[i].length; j++)
  43. A[i][j] /= P;
  44. for (int j = i + 1; j < A.length; j++) {
  45. P = A[j][i];
  46. if (Math.abs(P) > 0.0001) {
  47. for(int k = 0; k < A.length; k++) {
  48. double v = A[k][i];
  49. A[j][k] /= P;
  50. A[j][k] -= A[i][k];
  51. }
  52. }
  53. }
  54. }
  55. dump(A);
  56.  
  57. //backward
  58. for(int i = A.length - 1; i >= 0; i--)
  59. for (int j = i - 1; j >= 0; j--) {
  60. A[j][A.length] -= A[j][A.length] * A[j][i];
  61. A[j][i] = 0.0;
  62. }
  63. dump(A);
  64. }
  65. static void dump(double[][] a) {
  66. for(int i = 0; i < a.length; i++) {
  67. for(int j = 0; j < a[i].length; j++)
  68. System.out.printf("%6.3f ", a[i][j]);
  69. System.out.println();
  70. }
  71. System.out.println();
  72. }
  73. }
  74. /* end */
  75.  
Runtime error #stdin #stdout 0.02s 245632KB
stdin
Standard input is empty
stdout
start.
java.io.FileNotFoundException: renritsu.txt (No such file or directory)