Java command line arguments. Using * as an argument for multiplication
It's not Java, it's the shell (cmd
if you're on Windows) you are using that interprets *
as "all files and folders in the current directory".
So when your write:
java calc * 2 5
You will actually give your program the following arguments:
java calc file_1 file_2 ... file_n 2 5
Where file_1 ... file_n
are all files (and folders) in the current directory).
If you do not want your shell to interpret *
as all files you need (as you have noticed) to quote that argument.
Incorrect command line arguments number when passing `*`
The *
does a shell glob. So it'll expand to all the files in your current directory, which will be the arguments to your program, you have 65 files in your directory. You can see what's happening if you run echo *
You need to single quote the *
as in ./test '*'
(double quotes would work too) , this will prevent the shell expanding *
. A *
is given to your program in this case, the shell removes the single quotes.
If you want to evaluate expressions, you could do
./test 3 2 '*'
In this case your program receives 3 additional arguments separated so argv[1]
is 3
, argv[2]
is 2
and argv[3]
is *
Or you could do:
./test '3 2 *'
In this case, your program receives 1 additional argument, argv[1]
will be the string 3 2 *
Passing an asterisk to a command-line program makes filenames appear in argv
You have 2 options
- Escape each single special symbol with a backslash (
\*
) or - Double-quote the argument (as in
"*"
).
Passing * in as command line argument in simple Java Calculator
You can try passing the command line argument as a single string (Example: "2 3 +") with the below modified code.
import java.util.Arrays;
class CommandLineDemo {
public static void main(String[] args) {
String strArray = Arrays.toString(args);
strArray = strArray.replace("[", "").replace("]", "").replaceAll("[, ]", "");
String[] splits = strArray.split("");
int operand1 = Integer.parseInt(splits[1]);
int operand2 = Integer.parseInt(splits[2]);
char theOperator = splits[3].charAt(0);
System.out.print(splits[1] + " " + splits[3] + " " + splits[2] + " = ");
switch(theOperator) {
case ('+'):
System.out.println(operand1 + operand2); break;
case ('-'):
System.out.println(operand1 - operand2); break;
case ('*'):
System.out.println(operand1 * operand2); break;
case ('/'):
System.out.println(operand1 / operand2); break;
default:
System.out.println("Invalid Operator selected");
}
}
}
The usage & output looks like this:
C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 +"
2 + 3 = 5
C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 -"
2 - 3 = -1
C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 *"
2 * 3 = 6
C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 /"
2 / 3 = 0
C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 a"
2 a 3 = Invalid Operator selected
C:\Users\sarath_sivan\Desktop>
Related Topics
How to Make Rjava Use the Newer Version of Java on Osx
Library to Read/Write Pbxproj/Xcodeproj Files
Compute Hex Color Code for an Arbitrary String
Aes Java Encoding, Ruby Decoding
Spring JSON Request Getting 406 (Not Acceptable)
Does Python Have an Equivalent to Java Class.Forname()
How to Connect to Ftps Server with Data Connection Using Same Tls Session
Expected Begin_Array But Was Begin_Object at Line 1 Column 2
Exception in Thread "Main" Java.Lang.Noclassdeffounderror: Org/Openqa/Selenium/Webdriver
How to Add a Shortcut Key for a Jbutton in Java
How to Get Pid of Process I'Ve Just Started Within Java Program
What Is the Meaning of $ in a Variable Name
Java: How to Call Non Static Method from Main Method
How to Properly Shutdown Java Executorservice