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.

3

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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);
  }
}

Comments

-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.

4 Comments

Are you sure the param comes in as a Vector of Strings?
... 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.
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).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.