Back to General discussions forum
Hello. I am a novice Java programmer who recently switched to Java from C++, and I had a question about inputting the test case data for the Problems. In C++, I was able to directly cut and paste multiple lines of data, and cin would realize the line breaks and store data accordingly, but with Java's Scanner.nextLine(), it seems that when I copy paste material it treats the entire block as one input.
Sorry if this question has a simple solution or has been answered before, I just haven't been able to find anything thus far.
Thanks.
Just to clarify my question, for example, if I have 3 scanner.nextLine() statements intended to each take 1 of three lines of test data, how do I make it so that the first line goes to the first statement, and likewise for the second and third when I copy and paste the test data to the command window?
Hi,
Sorry, but until a Java expert comes along, you're stuck with me :-)
I wouldn't recommend pasting into the command window.
How were you doing it in C++? What I'd suggest people do is copy/paste the input data into a text file (I use input.txt), then tell C++ to read stdin from input.txt. How you do that will depend on which C++ you are using. In the version of MS Visual C++ that I use, I have to go to Project...Properties...Debugging...Command Arguments and set it to
<input.txt
For Java, I think you should do something similar. Does this help at all? http://stackoverflow.com/questions/188547/eclipse-reading-stdin-system-in-from-a-file
That helps. I just always remember with C++ that I'd paste it directly into the command line and use cin, but that doesn't work with Java. Now I just need to learn how to read .txt files with Java.
Thanks again.