1

When I pass in: "*" 3 5 I expect "*" to be stored in argv[0], 3 in argv[1], and 5 in argv[2]

But instead, it will store .classpath, .project, .settings, bin, src, 3, 5 making argv.length == 7 instead of 3.

I know the problem is occurring at argument input. I can solve this by having the user format the argument input but that will not suffice as the format for the input is an asterisk in double quotes. I am able to pass in an asterisk in single quotes, but the problem is when it is passed in with double quotes (using Eclipse >> run configurations >> arguments >> "*" 3 3) "*" will pass in a directory instead of the string asterisk in double quotes.

How do I get asterisk in double quotes to be read in as a string? I know it can be done, I'm just trying to figure out why it is being read in as a directory. Following any other example, double quotes should resolve the problem but not so in this case.

/**
 * Drives the program.
 * @param argv arguments from command line
 */
public static void main(final String[] argv) {
    final TableType type;
    final int       start;
    final int       stop;
    for(int i=0; i<argv.length; i++){
        System.out.println(argv[i]);
    }

    if (argv.length != 3) {
        usage();
    }
    System.out.println(argv[0]);
    type  = getType(argv[0]);
    start = getNumber(argv[1]);
    stop  = getNumber(argv[2]);

    displayTable(type, start, stop);
}

/**
 * Returns the TableType represented by the specified String.
 * @param str the table type
 * @return TableType
 */
public static TableType getType(final String str) {
    final TableType type;
    if (str.equals("+")) {
        type = TableType.ADD;
    } else if (str.equals("\"*\"")) {
        type = TableType.MULT;
    } else {
        usage();
        type = null;
    }
    return (type);
}
  • can you show us your command line and also: which operating system / shell are you using? – Jean-François Fabre Sep 23 '16 at 19:43
  • 2
    "I can solve this by having the user format the argument input but that is not reasonable" - how is that not reasonable? If you want the user to provide command-line arguments, they're going to have to provide them in the correct format, proper escaping and all. – user2357112 Sep 23 '16 at 19:43
  • 2
    By "pass in" I suppose you are talking about command-line arguments. In that case, this isn't really a Java question at all, but rather a question of the behavior of the shell from which you launch `java`. Probably quoting the asterisk will solve that problem. – John Bollinger Sep 23 '16 at 19:44
  • 1
    Your shell will replace * with directories and files in your working directory. – Peter Lawrey Sep 23 '16 at 19:46
  • 2
    Possible duplicate of [Problem of \* in Command line argument](http://stackoverflow.com/questions/2718873/problem-of-in-command-line-argument) – azurefrog Sep 23 '16 at 19:47
  • I'm testing it on Eclipse >> Run Configurations. – vincent-h-lee Sep 23 '16 at 19:50
  • I've clarified my problem on the main question. I can solve this by having the user format the argument input but that will not suffice as the format for the input is an asterisk in double quotes. I am able to pass in with single quotes or with a series of escape characters but how do I get asterisk in double quotes to be read in as a string? I know it can be done, I'm just trying to figure out why it is being read in as a directory. Following any other example, double quotes should resolve the problem but not so in this case. – vincent-h-lee Sep 23 '16 at 20:08
  • Sorry but by "asterisk in double quotes to be read in as a string" you mean you want to include also double slashes in `args[0]` or only `*`? If it is first case then ``\"*\"`` should do the trick. If latter then I don't know any solution yet (but this shouldn't be problem outside of Eclipse, maybe instead of passing `*` in RunConfiguration make your app read arguments explicitly from some file?). – Pshemo Sep 23 '16 at 20:32

0 Answers0