I am trying to do an assignment for school and I don't know how to deal with the input. I have provided a link below for context on the assignment:
https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B1DkmkmuB-leNDVmMDU0MDgtYmQzNC00OTdkLTgxMDEtZTkxZWQyYjM4OTI1&hl=en
I have a general idea on how to do everything the assignment asks, but I'm unsure of how to deal with input.
A sample input is:
a0
0 a00 ab000
Which gives an output of:
Tree 1:
Invalid! Tree 2: height: -1 path length: 0 complete: yes postorder: Tree 3: height: 0 path length: 0 complete: yes postorder: a Tree 4: height: 1 path length: 1 complete: yes postorder: ba
I intend to do this with Java. My question is how do I enter multiple lines of input like in the sample into the Windows cmd.exe line when not piping in an input file? Because pressing enter would just run the program with one line of input instead of making a new line. Also, since the assignment is being marked automatically, the input can't be interactive, so how would I go about knowing when to 开发者_运维问答stop reading?
Thanks.
From the assignment:
You can assume that input will come from standard input in a stream that represents one string per line. In reality, the input will come from a file that is piped to standard in. Output should be sent to standard out. A sample input and output file are available.
Just read System.in and write to System.out. Since the input will be piped to stdin, you will get EOF at the end of the input file.
When interacting via the CMD window, use Ctrl-Z to indicate EOF (on Windows) or on a Linux system, use Ctrl-D
If you can use System.in, then you could use an InputStreamReader that reads from a stream of System.in. Then, use a BufferedReader to get each line using readLine() method. For example, look at this code:
InputStreamReader input = new InputStreamReader(System.in)
BufferedReader reader = new BufferedReader(input);
while (reader.readLine()) {
//Your code here. It will finish when the reader finds an EOL.
}
This code will work without any problem -
Scanner sc = new Scanner(System.in);
String bitstring="";
while(sc.hasNextLine()){ //until no other inputs to proceed
bitstring=sc.nextLine();//save it to the bitstring
//proceed with your other codes
}
精彩评论