Search code examples
javabufferedreader

What's the correct parameter value of System.getenv("OUTPUT_PATH")?


I'm joining Hackerrank for the first time. Just for some practise purposes. Then, I found this line

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

and pretty confused about what's the correct replacement for the "OUTPUT_PATH". Because the code was copied into my IDE (Eclipse) I read the documentation but none of the reserved values suited. They all throwed NPE.

What's the correct parameter value of the System.getenv("..") in my case?

In case you need the full code:

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.JOptionPane;
public class Solution {

    /*
     * Complete the simpleArraySum function below.
     */
    static int simpleArraySum(int[] ar) {
        return 2;

    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        //name − This is the name of the environment variable.
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arCount = Integer.parseInt(scanner.nextLine().trim());

        int[] ar = new int[arCount];

        String[] arItems = scanner.nextLine().split(" ");

        for (int arItr = 0; arItr < arCount; arItr++) {
            int arItem = Integer.parseInt(arItems[arItr].trim());
            ar[arItr] = arItem;
        }

        int result = simpleArraySum(ar);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();
    }
}

Solution

  • As I said in the comments, you're not supposed to replace it. That's an environment variable in the shell that is used to run your solution.

    Seeing as the value of the variable is passed to FileWriter, this means that it represents the name of a file.

    You can replicate it in a terminal by running your program with the command:

    env OUTPUT_PATH=/path/to/some/file java Solution
    

    This will start a new shell which contains a variable called OUTPUT_PATH pointing to a file called /path/to/some/file and when the program starts, the file name will be used