-1

I need to make program, that gets optional arguments from command line. In case there shows flag -regex i need to read folowing regex expression and save it to program as a string. Something like: java Main -regex *.java

What I have now looks like:

public static void main(String[] args) 
...
if (args[i].equals("-regex")) {
   String myRegex = args[i+1];
   System.out.println(myRegex);
   i++;
   i++;

}
...

But, as I run program from folder, where are some files as file1.java and file2.java, the program prints "file1.java". What can i do to get my variable with "*.java"?

When I tried to print all the args, with:

for (String arg:args) {
    System.out.println(arg);
}

it gives me:

-regex
file1.java
file2.java

Which leads me to conclusion, that I need to read the args differently...

dunajovm
  • 1
  • 1
  • 1
    `if (args[i] == "-regex")` [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo Dec 26 '21 at 10:31
  • Yes, I dont have it exactly like this, I have it in switch... so I wrote it little differently here. Fixed – dunajovm Dec 26 '21 at 10:40
  • 2
    Only that code will not let us reproduce your problem of *infinite loop*. To get proper answer we would need code which can be debugged so please provide [SSCCE](http://sscce.org) (a.k.a. [MCVE]) – Pshemo Dec 26 '21 at 10:42
  • The problem of infinite loop is caused by the fact, that I iterate trought `args` by enlarging *i* in while cycle. And as the *i* can be enlarged only in *if* block, and the `*.java` provides multiple arguments (not only one argument after the `-regex` part, but all the matching files), I couldnt get to them and enlarge *i*. So the while cycle runs indefinitely. – dunajovm Dec 26 '21 at 11:12
  • That part I already solved. But still need to know how to read the regex properly as a string. The fact, that when I let print all the args, it gives me already all the matching files (if there are any, else gives me my regex) gives me suggestion, that maybe I need to read it differently, or it doesn't have any solution. – dunajovm Dec 26 '21 at 11:19
  • If you want your application to get `*.java*` string instead of series of `file1.java file2.java` then you need to tell console that this is textual data. To do so simply wrap it in quotes like `java Main -regex "*.java"` (notice quotes surrounding `*.java`). – Pshemo Dec 26 '21 at 12:58
  • BTW `*.java` does *not* represent regex (regular expression). In regex to match String which ends with `.java` you would need to write something like `.*[.]java` or `^.*[.]java$` (depending if you want to just find some part which matches regex, or check if whole string matches regex). – Pshemo Dec 26 '21 at 13:15

3 Answers3

1

You need to escape asterisk for the shell / terminal you use, as the shell is performing file name expansion before running the java application. For bash you can use backslash, this should pass *.file as args[1]:

java -cp your.jar your.Main -regex \*.file

Using quote also works to escape the default file expansion in some shells, and fixes when using Windows CMD.EXE:

java -cp your.jar your.Main -regex "*.file"
DuncG
  • 12,137
  • 2
  • 21
  • 33
0

In Java, use string.compareTo(string) to compare strings instead of the ==-operator:

    package com.main;
    
    public class Main {
        public static void main(String[] args) {
            if (args[0].compareTo("-regex") == 0) {
                String myRegex = args[1];
                System.out.println(myRegex);
            }
        }
    }
draz
  • 793
  • 6
  • 10
ErayZaxy
  • 66
  • 2
  • 8
  • This should work, but you should also check if args.length > 1 to make sure the program doesn't crash – ErayZaxy Dec 26 '21 at 10:36
  • Still prints "file1.java", when running like `java Main -regex *.java`. But thank you for suggestion. Works only when the provided regex doesn't match any file in folder. – dunajovm Dec 26 '21 at 10:48
0

You will need to enclose the Command Line command double (") quotation marks, for example:

java Main "-regex *.java"

then use something like this:

String myRegex = "";
for (int i = 0; i < args.length; i++) {
    if (args[i].startsWith("-regex")) {
        String[] commandParts = args[i].split("\\s+");
        if (commandParts.length > 1) {
            myRegex = commandParts[1];
            System.out.println(myRegex);
        }
    }
} 

The console will display *.java when the application starts.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22