user3068177
user3068177

Reputation: 357

Writing to a file from a void method

I am working on a program that uses Dijkstra's algorithm and records the results to a text file. The bit of code I have that writes to the file looks like this:

try (PrintWriter pr = new PrintWriter(filename + "Out.txt")) {
                pr.println("Adjacency Matrix: " + (endTime - startTime) + " ms ");
                pr.println("Min-Heap: ");
                pr.println("Fibonnaci Heap:");
                pr.println("Dijkstra Adjacency Matrix");
                pr.println(g.printPath(END));
    }
        } catch (Exception e) {

        }

I have no problems with this bit of code, except for the line g.printPath(END). The error I receive is "void type not allowed here". I fully understand what this means. It occurs because the printPath method is void. It looks like this:

public void printPath(String end) {
    if (!graph.containsKey(end)) {
        System.err.printf("End vertex is not contained within graph \"%s\"\n", end);
        return;
    }

    graph.get(end).printPath();
    System.out.println();

}

Since I need access to the variable it would print, I tried to modify it to have a return type that I could write to the text file. What I came up with was this:

public String printPath(String end) {
    if (!graph.containsKey(end)) {
        System.err.printf("End vertex is not contained within graph \"%s\"\n", end);
        return null;
    }

    graph.get(end).printPath();
    System.out.println();
    return graph.get(end).printPath();

}

This again has errors, since the method is of type string but graph.get(end).printPath() is void (the get method is also void). I have attempted returning other variables such as graph and graph.get(end) but they do not return the actual variables inside the graph. I know that graph.get(end).printPath() prints out the correct values I want. I am just struggling to find a way to store them. Is there an easy way I can write this to the text file that I am overlooking without having to go back and edit all of my methods to make them not void? Thanks!

Upvotes: 0

Views: 2996

Answers (2)

Paul Hicks
Paul Hicks

Reputation: 13999

With your current usage, printPath shouldn't be printing anything: maybe you could even rename it to getPath. You need to build a string with the correct value and return it, so that the returned value can be passed to println.

public String printPath(String end) {
    if (!graph.containsKey(end)) {
        return "End vertex is not contained within graph \"%s\"\n", end);
    }

    // Also rework this to return a string instead of printlning stuff.    
    return graph.get(end).printPath();
}

Alternatively, don't pass the value to println and just call g.printPath(END); directly.

try (PrintWriter pr = new PrintWriter(filename + "Out.txt")) {
     pr.println("Adjacency Matrix: " + (endTime - startTime) + " ms ");
     pr.println("Min-Heap: ");
     pr.println("Fibonnaci Heap:");
     pr.println("Dijkstra Adjacency Matrix");
     g.printPath(END);
} catch (Exception e) {
}

Upvotes: 1

chengpohi
chengpohi

Reputation: 14217

There is a way to do it by redirectSystem.out.print:

public String printPath(Graph graph, String end) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(bos);
    //set output stream to bos to capture output
    System.setOut(printStream);

    graph.get(end).printPath(); //your output
    System.out.println();

    //reset output stream to file descriptor
    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
    return bos.toString();
}
  1. Redirect the System.out to ByteArrayOutputStream,
  2. Start print
  3. Reset the System.out to FileDescriptor

Finally, Really don't suggestion do it, it's dirty code and important it's not thread-safe, and it's confusing. There is a suggestion about how to deal this:

  • Create a method to formatt graph.get(end) and return correct String type path

Upvotes: 1

Related Questions