开发者

how can i take in a char as an command line argument in java

开发者 https://www.devze.com 2023-02-13 11:48 出处:网络
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();

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();

     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))开发者_JAVA百科;

thanks in advance guys!!! :))))


You need to invoke the String argument's charAt method...

args[1].charAt(0)


If you know your input string is EXACTLY one character long, use str.charAt(0);


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.

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.


  1. 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];

  2. Convert first letter of String s into a char with char ch = s.charAt(0);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号