java 我如何在java中将char作为命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5130110/
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
how can i take in a char as an command line argument in java
提问by Java Beans
how can i take in a char as an command line argument in java. I know how to do it for an integer and double which get converted to strings, but am stuck on character....I was thinking using charAt();
我如何在 java 中将字符作为命令行参数。我知道如何将整数和双精度转换为字符串,但我被困在字符上......我正在考虑使用 charAt();
double x = Double.parseDouble(args[0]);
// i know the below line doesnt make sense because charAt(c) expects 'c' to be the actual
// character and i have an index. but its what i wish to do(if it makes any sense).
char op = charAt(args[1]);
double y = Double.parseDouble(args[2]);
//for (Operation op : jumpTable)
System.out.printf("%f %s %f = %f%n", x, op, y, myArray[op].Compute(x, y));
thanks in advance guys!!! :))))
提前谢谢你们!!!:)))))
回答by mmccomb
You need to invoke the String argument's charAt method...
您需要调用 String 参数的 charAt 方法...
args[1].charAt(0)
回答by corsiKa
If you know your input string is EXACTLY one character long, use str.charAt(0);
如果您知道您的输入字符串正好是一个字符长,请使用 str.charAt(0);
回答by Hovercraft Full Of Eels
All command line arguments are Strings. So the solution is how to get a char from a String, and yes charAt(..) works well for this.
所有命令行参数都是字符串。所以解决方案是如何从字符串中获取字符,是的 charAt(..) 对此很有效。
edit: Your assumptions in your code are wrong since charAt takes an int input, the index of the char, not a char. You will want to read the String API to see the details.
编辑:您在代码中的假设是错误的,因为 charAt 需要一个 int 输入,即字符的索引,而不是字符。您将需要阅读 String API 以查看详细信息。
回答by Pravin Wadkar
Take an argument into String because the default JVM provides command line args as a String[] and you can get the first arg with
String s = args[0];
Convert first letter of
String s
into achar
withchar ch = s.charAt(0);
将参数带入 String 因为默认 JVM 将命令行参数提供为 String[] 并且您可以获得第一个参数
String s = args[0];
将的第一个字母
String s
转换为char
withchar ch = s.charAt(0);