(Java) How to make your own test files to run your program against?

(Edited) I’m currently using Java and IntelliJ as an IDE. During a contest, I want to be able to test my code against my own inputs. Because I am only able to test my code against the inputs USACO gives me. So I want to make a file that I can run my code on. How do I make a file that I can run my Java program on? For example, if I want to test the simple:

I can make a file:
Hello World!

And run my program on this file, to make the program output: “Hello World!”

Thank you so much in advance for all responses!

So… what’s your question? Are you just asking how to do file IO? If so, I’m pretty sure that’s covered in the USACO guide.

Ah. I fear I have not stated my question clear enough.
How can I test my code, against my own custom input? Because during the contest, I can only test my code against the input they give me. I want to be able to create my own file with my own input where I can test my code against, and my question is how to do so.

Use file input output for a file:
import java.io.*;

BufferedReader br = new BufferedReader(new FileReader("filename.in"));
PrintWriter pw = new PrintWriter(new FileWriter("filename.out"));

When you want to submit to contest, just replace with (since the website uses standard I/O):

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

No, no, I do not mean how to do I\O for a contest. I already know how to do that. What I am asking is, how to be able to make my OWN files that I can test my code against. For testing purposes, I want to be able to make my own inputs, and not just use the inputs USACO gives me.

Why don’t you know how to make your own inputs?

Can’t you just run your code on your IDE?? Alternatively, you can test your solution here. This custom invocation also gives feedback on the time it took… Does that answer your question?? Also, why do you need a file? Can’t you just write your own inputs and see if you get the right output (you can check your output by solving by hand).

Next time, please clarify your question… That was very ambiguous and led a lot of users (including myself) to question what you meant.

If you mean how do you test your code, as jessechoe10 said, you just create your own test cases and solve by hand. Then compare what your code gives to what you got by hand.

If you don’t know how to run code in IntelliJ, the simplest way is to just right-click your main method and click run.

1 Like