0

For example, the user run my programme like this

myprogram -p1 my_name, -p2 my_address, -p3 my_gender

But the user may type this, and it still valid:

myprogram -p2 my_address, -p1 my_name,-p3 my_gender

How can I parse it in Java? Thanks.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195
  • Checkout this answer for libraries to do it http://stackoverflow.com/a/1524690/898289, or just go it alone and check each of the args array you get in main(String [] args)... – Adam Apr 18 '12 at 10:07
  • possible duplicate of [Java library for parsing command-line parameters?](http://stackoverflow.com/questions/1200054/java-library-for-parsing-command-line-parameters) – Kilian Foth Apr 18 '12 at 10:07
  • http://stackoverflow.com/questions/3309250/command-line-parsing-commons-cli-alternatives – assylias Apr 18 '12 at 10:08

3 Answers3

1

You can use something like this:

public static void main (String[] args) {
      for(int i = 0; i < args.length; i++) {

                if(args[i].equals("-p1")) {
                    // args[i+1] contains p1 argument

                } else if(args[i].equals("-p2")) {
                    // args[i+1] contains p2 argument
                }

      }
}

Make sure to check whether the i+1 argument is there, otherwise an exception will be thrown.

There are more advanced methods of going this, you could e.g. use hashing to map the flag to the processing function. But, for this purpose, I guess this will do.

What I do not understand is the use of comma's in your sample. What are they used for?

Jonny5
  • 1,390
  • 1
  • 15
  • 41
0

If you're looking for a DIY, then maybe this might be starting point.

public class Foo
{
  private static final String[] acceptedArgs = { "-p1", "-p2", "-p3" };

  public void handleCommandArgs(String... args)
  {
    if (args != null)
    {
      for (int argIndex = 0; argIndex < args.length; argIndex++)
      {
        for (int acceptedIndex = 0; acceptedIndex < acceptedArgs.length; acceptedIndex++)
        {
          if (args[argIndex] != null && args[argIndex].equals(acceptedArgs[acceptedIndex]))
          {
            String arg = args[argIndex], param = args[argIndex + 1];
            performRoutine(arg, param);
          }
        }
      }
    }
  }
  private void performRoutine(String arg, String param)
  {
    System.out.println(arg + " ->" + param.replace(",", ""));
  }

  public static void main(String[] args)
  {
    (new Foo()).handleCommandArgs(args);
  }
}
Bitmap
  • 12,402
  • 16
  • 64
  • 91
-3

Sample from Java Tutorials @Oracle

public static void main (String[] args) {
    for (String s: args) {
        System.out.println(s);
    }
}

The params come in a vector of String.

Pedro Ferreira
  • 629
  • 3
  • 8
  • Are you sure the param comes in as a Vector of Strings? – Bitmap Apr 18 '12 at 11:18
  • ... So dispite the question having several similar questions answered, you choose to badmouth and downvote when i provided a valid way of parsing the command line over flagging it as duplicated question. Do realize i did not stated class Vector. I used the word vector to state it was the standard way of params to reach the application because yes the words come lined up in a vectorial way. And on a constructive side, i've posted also a link where he can learn instead of just copy&paste a solution. – Pedro Ferreira Apr 18 '12 at 11:35
  • 1
    please learn the technical vocabulary - that's what the downvotes tell you :-) In communication, it's not helpful to invent/assume words for anything that _has_ a definite descriptor, especially not if that word has another connotation in that context (here: java). – kleopatra Apr 18 '12 at 11:40
  • Not even gonna bother to give further answer. In communication one should not just give the answer but to explain so the person who did the question actually learns and won't learn by reading a whole enchilada of code with a typo in the middle. – Pedro Ferreira Apr 18 '12 at 11:47