List of strings (multiple lines) as command line input in Java

I am trying to complete an assignment for a school, and I do not know how to handle this. I have provided the link below for the context of the assignment:

https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B1DkmkmuB-leNDVmMDU0MDgtYmQzNC00OTdkLTgxMDEtZTkxZWQyYjM4OTI1&hl=en

I have a general idea of โ€‹โ€‹how to do everything that assigns the task, but I'm not sure how to handle the input.

Input Example:

a0
0
a00
ab000

Which gives the result:

Tree 1:
Invalid!
Tree 2:
height: -1
path length: 0
completed: yes postorder:
Tree 3:
height: 0
path length: 0
completed: yes postorder: a
Tree 4:
height: 1
path length: 1
completed: yes postorder: ba

I intend to do this using Java. My question is, how can I enter several lines of input, as in the example, in the cmd.exe line of Windows, if they are not connected in the input file? Because pressing enter just launches the program with one line of input instead of creating a new line. Also, since the assignment is assigned automatically, the input cannot be interactive, so how do I know when to stop reading?

Thanks.

+6
source share
3 answers

From the assignment:

You can assume that the input will come from standard input in a stream that represents one line in a line. In reality, input will be received from which is connected to the standard. The output should be sent to the standard version. Sample input and output available.

Just read System.in and write to System.out. Since the input will be passed to stdin, you will get EOF at the end of the input file.

When interacting through the CMD window, use Ctrl-Z to specify EOF (on Windows) or on a Linux system, use Ctrl-D

+2
source

If you can use System.in, you can use an InputStreamReader that reads from the System.in stream. Then use BufferedReader to get each line using the 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. } 
+1
source

This code will work without problems -

 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 } 
+1
source

Source: https://habr.com/ru/post/887334/


All Articles