1

I've made a program for determining the time of a murder. The code I have right now prints exactly what it should to the terminal. I want it printed to a file. However, I'm not supposed to use System.setOut() for this assignment. I'm supposed to print to a file instead of to the terminal.

I know how to write a simple String into a file, the problem here is I already have methods for printing my results to the terminal, and I'm not sure how I "convert" those methods into printing into a file instead.

This is my two printing methods and main method:

Printing 2d array method:

public static void printArray2d(String[][] array2d){
    for(int i = 0; i < array2d.length; i++){
        for(int j = 0; j < array2d[i].length; j++){
            System.out.print(array2d[i][j]);
        }
        System.out.print("\n");
    }
}

Printing full report method:

public static void printReport(String[][] array2d, double arrayMin, double arrayMax){
    System.out.println("Time since death probability distribution");
    double hours = (arrayMax-arrayMin)/(array2d.length-1);
    System.out.printf("Each line corresponds to approximately %.2f hours\n", hours);
    System.out.printf("%.2f hours\n", arrayMin);
    printArray2d(array2d);
    System.out.printf("%.2f hours\n", arrayMax);
}

Main method:

public static void main(String args[]) {

    double[] array = cooldownSamples(27, 1000);
    double[] counts = countsFromArray(array, 20);
    String[][] array2d = array2dFromCounts(counts);
    printReport(array2d, minFromArray(array), maxFromArray(array));
}

I can post the entire code if needed.

I am aware that there are similar questions asked earlier, but none of them gave me the help I needed. I also do not have enough reputation to ask follow-up questions to any of the answers given on those threads, so I was forced to ask a new question.

Thanks in advance for any help given!

Additional information:

Even though I said I'm not supposed to use System.setOut(), I have tried using the method and answers given in this thread, without any luck. If the best and most efficient way of doing this is via System.setOut(), I do appreciate answers that make me understand how I can implement this and make it work in my code, even though I'm looking for an alternative method.

  • if `your_process` prints to the console, then you can easily print that to a file with `your_process > outfile`. – Seelenvirtuose Oct 19 '17 at 10:00
  • I don't get it. What is the problem with `System.setOut()` since you are ask to "_to print to a file instead of to the terminal_". `System.out` is just a stream, originally link to the terminal, if you redirect it to a file, this would be correct, it will not pass throught the terminal at all. – AxelH Oct 19 '17 at 10:01
  • You can redirect the output (suggested above), you can use System.out + System.setOut, or you can change all of your System.out statements to use a different writer/PrintStream. – matt Oct 19 '17 at 10:03
  • @AxelH I don't have a problem with it, myself. However, the assignment states I shouldn't use it. I would still appreciate an answer making use of `System.setOut()`, giving me an understanding of it, and making my code work with it. Sorry for the inconvenience though. – Jonas Norill Oct 19 '17 at 10:04
  • It state not to use specificly `System.setOut` or just like you said "_I'm supposed to print to a file instead of to the terminal_" ? – AxelH Oct 19 '17 at 10:06
  • @AxelH the assignment says I'm supposed to print to a file instead of to the terminal. The teacher told me to not use `System.setOut()` yet, because we haven't been through it. I wouldn't fail the assignment if I did, though. – Jonas Norill Oct 19 '17 at 10:14

1 Answers1

1

It is really simple: right now, you are using a (static) object System.out to do all printing. This object has methods to print, println, and so on.

Instead of using System.out, you create an instance of say PrintWriter and call method on that object, like

PrintWriter writer = new PrintWriter("whatever.txt");
writer.println("whatever");
writer.close();

That is all there is to this. Or even simpler, you could instantiate a PrintStream object. You could then do things such as:

 PrintStream out = System.out // or new PrintStream("filename");
 doStuff(out);

... with:

 public void doStuff(PrintStream out) {
   out.println...

And now you have one central place where you decided if you want to print to System.out - or somewhere else!

GhostCat
  • 137,827
  • 25
  • 176
  • 248