I am new in Java and I want program to get as argument a different sign. For example:
java program #
And in program I convert string to char like this:
char c = args[0].charAt(0);
or like this:
char [] c = args[0].toCharArray();
But when I put *, it doesn't work. I print and get D or even name of file.
Asked
Active
Viewed 53 times
-2
kelp_sk
- 1
- 2
-
What do you mean by "when I put *, it doesn't work"? What is your program supposed to do? Please show us some code. – user Jun 29 '20 at 17:00
-
Many shells will expand `*` in a command into a list of files. This is outside Java's control. You can put quotes, `"*"`, to avoid the expansion. – khelwood Jun 29 '20 at 17:00
-
I try to run program in cmd, first I compile by command ```javac Program.java``` and then: ```java Program *```, where star is a first argument – kelp_sk Jun 29 '20 at 17:04
-
I cannot use quotes – kelp_sk Jun 29 '20 at 17:07
-
Why can't you use quotes? – RealSkeptic Jun 29 '20 at 17:35
-
Because its a university assignment – kelp_sk Jun 29 '20 at 17:41
-
That's irrelevant. The exercise probably doesn't tell you how to run the program, only what it should do. – RealSkeptic Jun 29 '20 at 17:48
1 Answers
0
When you use * character in the command line, it becomes expanded - that's why you get a filename/list of filenames.
You may try escaping asterisk:
- using backslash
\:java program \* - using double quotes
"(which should also be escaped):java program \"*\"However, in both cases you're likely to get the asterisk along with the escape character.
Other solutions to similar issue are shown here.
Nowhere Man
- 18,291
- 9
- 17
- 38