简体   繁体   English

字符串列表(多行)作为 Java 中的命令行输入

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

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 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 a0
0 0
a00 a00
ab000 ab000

Which gives an output of:这给出了 output :

Tree 1:树1:
Invalid!无效的!
Tree 2:树2:
height: -1高度:-1
path length: 0路径长度:0
complete: yes完成:是
postorder:后购:
Tree 3:树 3:
height: 0高度:0
path length: 0路径长度:0
complete: yes完成:是
postorder: a后购:一个
Tree 4:树 4:
height: 1高度:1
path length: 1路径长度:1
complete: yes完成:是
postorder: ba后购:ba

I intend to do this with Java.我打算用 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?我的问题是,当不在输入文件中进行管道传输时,如何将示例中的多行输入输入 Windows cmd.exe 行? 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.您可以假设输入将来自 stream 中的标准输入,该输入代表每行一个字符串。 In reality, the input will come from a file that is piped to standard in. Output should be sent to standard out.实际上,输入将来自通过管道传输到标准输入的文件。Output 应该发送到标准输出。 A sample input and output file are available.示例输入和 output 文件可用。

Just read System.in and write to System.out.只需阅读 System.in 并写入 System.out。 Since the input will be piped to stdin, you will get EOF at the end of the input file.由于输入将通过管道传输到标准输入,因此您将在输入文件的末尾获得 EOF。

When interacting via the CMD window, use Ctrl-Z to indicate EOF (on Windows) or on a Linux system, use Ctrl-D通过 CMD window 进行交互时,使用 Ctrl-Z 指示 EOF(在 Windows 上)或在 Linux 系统上,使用 Ctrl-D

If you can use System.in, then you could use an InputStreamReader that reads from a stream of System.in.如果您可以使用 System.in,那么您可以使用从 System.in 的 stream 读取的 InputStreamReader。 Then, use a BufferedReader to get each line using readLine() method.然后,使用 BufferedReader 使用 readLine() 方法获取每一行。 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM