Home > database >  How to print the result of a void type method into a file with Java PrintWriter
How to print the result of a void type method into a file with Java PrintWriter

Time:11-24

I'm doing exercise work for my Java programming course, the question goes as so:

Write a method called printTree that outputs to a file a triangle of asterisks based on the height value passed to the procedure. Test this method in the main method.

  • E.g. Triangle of height 3 should output to a file called file14:

I'm just not sure how to write the void return to the file that I've made in the main method. I'd like to minimize the importing of any other java.io methods aside from FileWriter, but any help is appreciated, Thanks.

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        output.write(printTree(4));
        
        //saving file
        output.close();
    }

    public static void printTree (int height) throws IOException{
        
        for (int i = 0; i < height; i  ) {
            for (int j = 0; j < height; j  ) {
                if (j < i) {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    } 
}

CodePudding user response:

Four observations. System.out is a PrintStream (and you could pass a PrintStream to your method). try-with-Resources allows you to eliminate explicit close() calls. Using System.getProperty("user.home") allows you to write into the home folder directly (which can be handy). And use j < i instead of if (j < i) in your inner loop. Like,

public static void main(String[] args) throws Exception {
    try (PrintStream output = new PrintStream(
            new File(System.getProperty("user.home"), "file14.txt"))) {
        printTree(output, 4);
    }
}

public static void printTree(PrintStream out, int height) throws IOException {
    for (int i = 0; i < height; i  ) {
        for (int j = 0; j < i; j  ) {
            out.print("*");
        }
        out.println();
    }
}

Also, since Java 11,

public static void printTree(PrintStream out, int height) throws IOException {
    for (int i = 0; i < height; i  ) {
        out.println("*".repeat(i)); // You might want "*".repeat(1   i)
    }
}

CodePudding user response:

You could solve it like this

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        output.write(printTree(4));
        
        //saving file
        output.close();
    }

    public static String printTree (int height) throws IOException{
        String output = "";
        
        for (int i = 0; i < height; i  ) {
            for (int j = 0; j < height; j  ) {
                if (j < i) {
                    System.out.print("*");
                    output  = "*";
                }
            }
            System.out.println();
            output  = "\r\n";
        }
        return output;
    } 
}

This is a somewhat ugly way to solve it quickly.

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        //output.write(printTree(4));
        printTree(4, output);
        
        //saving file
        output.close();
    }

    public static void printTree (int height, PrintWriter pw) throws IOException{
        
        for (int i = 0; i < height; i  ) {
            for (int j = 0; j < height; j  ) {
                if (j < i) {
                    System.out.print("*");
                    pw.write("*");
                }
            }
            System.out.println();
            pw.write("\r\n");
        }
    } 
}
  • Related