java 将命令行输入转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/777898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turn commandline input into an array
提问by phill
I'm working on a Bruce Eckel exercise on how to take a keyboard command line input and put it into an array. It's supposed to take 3 separate inputs and place them into an array and print them back out.
我正在做 Bruce Eckel 练习,关于如何获取键盘命令行输入并将其放入数组中。它应该接受 3 个单独的输入并将它们放入一个数组中并将它们打印出来。
Here is the code I have so far:
这是我到目前为止的代码:
//: object/PushtoArray
import java.io.*;
import java.util.*;
class PushtoArray {
public static void main(String[] args) {
String[] s = new String[3];
int i = 0;
//initialize console
Console c = System.console();
while ( i <= 2 ) {
//prompt entry:
//System.out.println("Enter entry #" + i + ": ");
//readin
s[i] = c.readLine("enter entry #" + i + ": ");
//increment counter
i++;
} //end while
//reset counter
i = 0;
//print out array
while ( i <= 2 ) {
System.out.println(s[i]);
i++;
} //end while
}//end main
} //end class
UPDATE:Now I get a different error:
更新:现在我得到一个不同的错误:
PushtoArray.java:15: readline(boolean) in java.io.Console cannot be applied to (java.lang.string)
s[i]= c.readline("Enter entry #" + i + ": ");
I'm trying to read in from the command prompt but it isn't prompting at all. It compiles correctly when I javac the java file.
我正在尝试从命令提示符读入,但它根本没有提示。当我对 java 文件进行 javac 时,它可以正确编译。
Am I using the wrong function? Should I be using a push method instead of an assignment?
我是否使用了错误的功能?我应该使用推送方法而不是赋值吗?
回答by Michael Myers
Are you sure you're running javacon the right file? There is no way that file could compile.
你确定你在javac正确的文件上运行吗?该文件无法编译。
You need a semicolon on the import statement:
import java.io.*;You forgot an end brace:
} // end main()There is no such method as
readline(). You need to read fromSystem.in. The easiest way is to make aScannerbefore the loop, then read from it in the loop. See the Javadocs forScannerfor an example.
您需要在导入语句上使用分号:
import java.io.*;你忘记了一个大括号:
} // end main()没有这样的方法
readline()。您需要从System.in. 最简单的方法是Scanner在循环之前创建一个,然后在循环中从中读取。有关Scanner示例,请参阅 Javadoc 。
Edit:See the Java Tutorialsfor more on reading from the command line. The Consoleclass(introduced in Java 6) looks like it has the readLine()method that you wanted.
编辑:有关从命令行阅读的更多信息,请参阅Java 教程。在Console类(Java 6中引入)看起来有readLine()你想要的方式。
Edit 2:You need to capitalize Line. You wrote "readline", but it should be "readLine".
编辑 2:您需要大写 Line。你写了“ readline”,但它应该是“ readLine”。
回答by Brian Agnew
Try
尝试
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
and then in your loop:
然后在你的循环中:
in.readline();
I would push the received lines into an ArrayList or similar. That way you can accept more/less than 3 lines of data (but that's only necessary if the number of lines you want to accept is variable)
我会将接收到的行推送到 ArrayList 或类似内容中。这样你就可以接受多于/少于 3 行的数据(但只有当你想要接受的行数是可变的时才需要)
EDIT: I thought originally the NoSuchMethod exception was highlighting a scoping problem. Obviously not, and thx to those who pointed that out!
编辑:我原以为 NoSuchMethod 异常突出了范围界定问题。显然不是,感谢那些指出这一点的人!
回答by Jani
//print out array
while ( i < 2 ) {
System.out.println(s[i]);
i++; //increase counter!
} //end while
回答by Jani
This might not be what you are looking for, but it takes three command line arguments, stores them into an array, then prints out the arguments from the new array:
这可能不是您要查找的内容,但它需要三个命令行参数,将它们存储到一个数组中,然后从新数组中打印出参数:
public class CommandArray {
public static void main (String[] args){
//Set up array to hold command line values
String[] arr = new String[3];
//Copy command line values into new array
for(int i = 0;i < 3;i++)
arr[i] = args[i];
//Print command line values from new array
for(int j = 0; j < 3; j++)
System.out.print(arr[j] + " ");
//Extra line for terminal
System.out.println();
}
}
Then, after you compile your code with javac CommandArray.java, you can execute it with java CommandArray Arg1 Arg2 Arg3.
然后,在使用 编译代码后javac CommandArray.java,可以使用 执行它java CommandArray Arg1 Arg2 Arg3。
Also, I noticed that, in your final while loop, you had:
另外,我注意到,在你最后的 while 循环中,你有:
while(i < 2) {
If the command line accepts 3 arguments, you would only print 2. The array indexes printed would be 0 and 1 since 1 < 2. You can change it to say:
如果命令行接受 3 个参数,您将只打印 2 个。打印的数组索引将为 0 和 1,因为 1 < 2。您可以将其更改为:
while(i <= 2) {
And don't forget to increment i.
并且不要忘记增加 i。

