Reputation: 339
I'm running a java program using
java myProgram < input.txt > output.txt
to read my test cases from an input file, and record the resulting output in an output file. For the most part it works great with no issues.
However, I'm unable to see the input that the user supposedly "typed" (ie the input that was read from the input.txt file). Instead, it just shows up as a blank space in my output file looking something like this:
Enter command: Command successfully entered!
Is there any way to force the input to be displayed, as it would be displayed to a real user? (like so, as if the user typed "Potato":)
Enter command: Potato
Command successfully entered!
Upvotes: 2
Views: 218
Reputation: 339
I ended up just using a boolean declared within the class of my test program to signal a debug mode, then just printing whatever had just been read into a string every time:
boolean debugEnabled = true;
Then, throughout my code:
if (debugEnabled)
System.out.println(inputString);
This was time consuming to go back and implement for every input, but worked perfectly.
Upvotes: 0
Reputation: 533700
The problem is that you don't want the input as it is read but rather as it is consumed. The difference is that since you are reading from a file, if you have a buffered input it will read say 8 KiB at a time. However, you might only consume a line at a time.
You can do this by overriding the input as early as possible in your code.
System.setIn(new InputWrapper(System.in));
However, you should try to ensure that a minimum is read, possibly no more than one line at a time.
Upvotes: 1