Results 1 to 9 of 9
  1. #1
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default Check for empty arguments

    This code doesnot work. How to check if args[1] is empty

    Java Code:
    if(args[1].equals=""){
    //statement
    }
    else {
    //statement
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    .equals is a method not a field. How do you use a method?

    Edit: You may also want to check, however, whether or not args[1] even exists, and whether or not it might be null, to avoid that statement itself throwing a couple of possible exceptions.

  3. #3
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    Can you please send me some tutorials regarding it

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Regarding what?

    How to use a method?
    How to get the length of an array?
    How to use an if statement?

    Those are all that are involved.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Hi,
    equals is a method .U have to use like this below.

    if(args[1].equals(""){
    //statement
    }
    else {
    //statement
    }

    But without aupplying args[1] value if u try to use the above code then u will get "ArrayIndexOutOfBoundsException"

  6. #6
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    I have to pass two arguments.
    If the second argument is empty it should not print any exception and it should return "";

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    802
    Rep Power
    6

    Default

    Hi,
    be specific with ur question.can u please send the code so that i can answer?

  8. #8
    sandy1028 is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    Default

    When I run the java program with one argument and if second argument is not passed it should return "" without giving an exception

    If the second argument is present then I have to assign as variable string as filename else I have to assign the value to ""
    ;

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Like I said, check that the length of the "args" array is at least 2 using length and an if statement, then use the equals method properly. Check your study notes and/or prior/example programs from your class to see how to use a method.

    Edit: Also, if the length of the array is not 2 or more, than, obviously, there is no second argument, so no reason to even attempt to use the equals method. And, if it is there, it is obviously not "empty" although it could be composed of all spaces by entering " " on the command line. If that needs to be checked for, then you are going to have to use the trim method and then the equals method (or better length method checking for more than 0) in succession.
    Last edited by masijade; 04-17-2009 at 01:47 PM.

Similar Threads

  1. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  2. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM
  3. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 08:36 PM
  4. Not Empty check of texboxes using javascript
    By Anju Jose in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-09-2008, 12:04 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:52 PM

Posting Permissions

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