Results 1 to 3 of 3

Thread: args[0] == "literal"

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    args[0] == "literal"

    I'm taking arguments from the command line, and I'm comparing them in if else statments to find certain arguments.
    Example:
    java myProg -arg1 subArg
    then I'll have for (int i=0; i<args.length; i+=2) {
    if (args[i] == "-arg1") { do something } else if (...
    else { defualt something }
    I always get the default something. args is a String array in my main method call. Problem comparing string literals with string array?

    NOMAD

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Its a problem with comparing all strings this way...
    Code:
    if (args[i] == "-arg1")
    == , when used with objects, compares the object pointers, ie - are they pointing to the same object. Use the equals() method instead.

    Code:
    if (args[i].equals("-arg1"))
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    thanks!

    NOMAD

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width