Buffered Writer to write .txt file

curiousX

The below code is for a project I have been working on. The output is required to be a .txt file. I cannot get the buffered writer to write when the while loop runs more than once. I have tried troubleshooting and testing and just cannot get it to work. If I do system.out.prints, the output is correct, so I believe that the issue lies with the buffered writer. I currently do not have a bw.close(), but I have tried putting that in multiple spots and it still did not work. The bw.write("hello")'s are just me trying to trouble shoot. Are my bw.flush()'s in the wrong spot?

My second question is, is this the best way to write the output to a .txt file? I am open to new ways and would love some suggestions to help broaden my knowledge of java.

public class Driver{


public static void main(String[] args) {

  String filepath;
  BufferedWriter bw = null;

  // TODO Auto-generated method stub
  try {
     System.out.println("To find the determinant of a Matrix, please enter the file below!");
     System.out.println("Please enter the file path of the txt file:\n");


     Scanner user_input = new Scanner(System.in);
     filepath = user_input.next();

     System.out.println("Filepath read: " + filepath);
     System.out.println("");
     int extCounter = filepath.indexOf('.');
     String Output_Path = filepath.substring(0, extCounter);

     // Read input file
     Scanner input = new Scanner(new File(filepath));

     //while loop to read in the text file
     while (input.hasNextInt()) {

        //find the size given by the first int
        //size is then used to allocate the array
        //to the correct size
        int size = input.nextInt();
        int[][] a = new int[size][size];

        for (int i = 0; i < size; i++) {
           for (int j = 0; j < size; j++) {

              try{
                 a[i][j] = input.nextInt();

              }
              catch (java.util.NoSuchElementException e) {
                 e.printStackTrace();
              }
           }
        }

        //Specify the file name and path here
        //the below code allows the user to enter one path
        //and get the output file at the same path
        //without having to enter it twice
        String OutFile;
        OutFile = Output_Path.concat("_Output_File15.txt");
        File file = new File(OutFile);

        /* This logic will make sure that the file 
         * gets created if it is not present at the
         * specified location*/
        if (!file.exists()) {
           file.createNewFile();
        }

        FileWriter fw = new FileWriter(file);
        bw = new BufferedWriter(fw);


        //print out the determinant by
        //calling the determinant method
        Deter deterMethod = new Deter();
        bw.write("The Determinant is: "
              + deterMethod.determinant(a) + " ...");
        int deterInt = deterMethod.determinant(a);
        System.out.println(deterInt);
        bw.write("" + deterInt);
        bw.write("hello");
        bw.write(deterInt);
        bw.flush();
        //deterInt = 0;
        //print a blank line 
        System.out.println();

     }
  } catch (Exception e) {
     e.printStackTrace();
  }
}





//determinant method
public class Deter
{
public static int determinant(int matrix [][]) { 

  //initializes the sum at 0
  int sum = 0;

  //base case 
  //1 x 1 matrix (x) is det(a) = x
  if (matrix.length == 1) {
     return matrix[0][0]; 

  } else {

     //for loop to cycle through the columns
     for (int j = 0; j < matrix.length; j++) {

        //equation to figure out the minor of the array given.
        //Deletes a row and column as provided by the definition
        //of a minor
        int[][] minor = new int[matrix.length - 1]
              [matrix.length - 1];

        //for loop to cycle through the rows
        for (int row = 1; row < matrix.length; row++) {
           for (int column = 0; 
                 column < matrix.length; column++) {


              if (column < j) { 
                 minor[row - 1][column] = matrix[row][column];
              } else if (column > j) {
                 minor[row - 1][column - 1] = matrix[row][column];
              }
           }
        }

        //recursive equation to get the 
        //sum to find the determinent 
        sum+=matrix[0][j]*Math.pow(-1,j)*determinant(minor);
     }
  }
  //return the sum to be printed out
  return sum;
} 
}
Scary Wombat

As you are creating a new FileWriter in each iteration of your loop, the file will get overriden, so either move the creation of the FileWriter before your loop or use the constructor which instructs to append

https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)

try modularizing your code by creating more methods

e,g,

 private Scanner getFileScanner () {

 Scanner user_input = new Scanner(System.in);
 String filepath = user_input.next();

 System.out.println("Filepath read: " + filepath);
 // Read input file
 Scanner input = new Scanner(new File(filepath));
 return input
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related