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();
}
}
I know that this question is bit old, but maybe somebody will have benefits from this answer...
This line:
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
reads the environment variable defined in Hackerrank runtime/testing environment to determine the place where results will be stored for further analysis.
In order to use exactly the same code, you have to create this variable on your system and use it, or change to store results on different place (as it is already explained in previous answers and comments).
But, ...
Since this is used for a Hackerrank solving, I think it is better to have all outputs redirected to system.out instead of file, because it is far more useful to see results at runtime (or debug) in the IDE console rather than places it in a file.
So, this line is better to change on this way:
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
which will stream your BufferedReader to System.out.
In that case You will have same output as it is on Hackerrank test cases, but in your IDE console instead in file.
That "OUTPUT_PATH" is an environmental variables. You have to declare that variable in your operating system to use it. Generally website like hackerrank do it because learning the path of there system is not good for security I guess. You can test your code in IDE but the environmental variables will be not there you need to declare. I hope it helped you for your confusion.
The question is already answered with a proper explanation. I am just adding the steps that you can simply use if your IDE is IntelliJ IDEA.
Step 1: When you run any main class in IDE, you can see there will be an option called Edit Configurations
like this
Step 2: Click on Edit Configurations. On click, you will see this.
Step 3: Click on 3 dots that are circled in the above image. On click, you will see this.
Step 4: Click on + symbol (as circled in the image) and add these environment variables as shown in the image. Once done click on the Apply button and you should be good to run your program.
This would be no different than the Hackerrank environment. Hope this helps.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With