Command line argument

Discussion in 'Java' started by joe, Dec 2, 2007.

  1. joe

    joe Guest

    Hi

    I was if " -v " flag means anything in comand line argument. In our
    project they told us to right the comman line argument in this
    forum...

    java WorPairCounter -vN FileName

    And N stands for either 1 or 2 . So were are asked to check if N is 1
    or 2 , so is there any way to check it ??
    joe, Dec 2, 2007
    #1
    1. Advertising

  2. On 2007-12-02 15:19:14 -0800, joe <> said:

    > Hi
    >
    > I was if " -v " flag means anything in comand line argument. In our
    > project they told us to right the comman line argument in this
    > forum...
    >
    > java WorPairCounter -vN FileName
    >
    > And N stands for either 1 or 2 . So were are asked to check if N is 1
    > or 2 , so is there any way to check it ??


    Command line arguments are passed as-tokenized to the main method as a
    string array. In your case, you'd get the following as your args array:

    {"-v1", "FileName"}
    or
    {"-v2", "FileName"}

    -o
    Owen Jacobson, Dec 2, 2007
    #2
    1. Advertising

  3. joe

    Arne Vajhøj Guest

    joe wrote:
    > I was if " -v " flag means anything in comand line argument. In our
    > project they told us to right the comman line argument in this
    > forum...
    >
    > java WorPairCounter -vN FileName
    >
    > And N stands for either 1 or 2 . So were are asked to check if N is 1
    > or 2 , so is there any way to check it ??


    public static void main(String[] args) {
    if(args.length == 2) {
    int version = 0;
    String fnm = null;
    for(int i = 0; i < args.length; i++) {
    if(args.startsWith("-v")) {
    version = Integer.parseInt(args.substring(2));
    } else {
    fnm = args;
    }
    }
    if(version > 0 && fnm != null) {
    System.out.println("version=" + version);
    System.out.println("fnm=" + fnm);
    } else {
    System.out.println("Usage: java WPCr -vn filename");
    }
    } else {
    System.out.println("Usage: java WPC -vn filename");
    }
    }

    Arne
    Arne Vajhøj, Dec 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fong
    Replies:
    3
    Views:
    1,160
  2. Thomas Kellerer
    Replies:
    5
    Views:
    34,572
    Nomak
    Apr 13, 2005
  3. Justin Naidl

    Command line argument problems

    Justin Naidl, Sep 17, 2004, in forum: C++
    Replies:
    2
    Views:
    440
    Niels Dekker - no reply address
    Sep 18, 2004
  4. nic977

    reading in command line argument

    nic977, Sep 12, 2003, in forum: C Programming
    Replies:
    13
    Views:
    607
    Dave Thompson
    Sep 22, 2003
  5. Steven Knight
    Replies:
    0
    Views:
    443
    Steven Knight
    Nov 7, 2003
Loading...

Share This Page