If i am providing two arguments over the command line args[0] and args[1]. How do individually check if either of the arguments are empty/null?
            Asked
            
        
        
            Active
            
        
            Viewed 3,994 times
        
    -3
            
            
        - 
                    1They wouldn't be present if they were empty / null. – Elliott Frisch Aug 22 '16 at 01:34
- 
                    But if i need to set them to a variable how do i check if they are null/empty? – Sam Aug 22 '16 at 01:35
- 
                    2check the length of the Strings args array – Scary Wombat Aug 22 '16 at 01:36
- 
                    So args[1].length() == 0? That still does not seem to be working. The only one i need to check if whether null is args[1], i don't care about arg[0]. – Sam Aug 22 '16 at 01:38
- 
                    so i should check if(args.length < 1)? This still does not seem to be working. – Sam Aug 22 '16 at 01:42
- 
                    2Define "not working". – Dave Newton Aug 22 '16 at 01:48
- 
                    @sam `so i should check if(args.length < 1)?` yes something like this (based upon your logic). – Scary Wombat Aug 22 '16 at 01:58
- 
                    The argument strings will not be null. The `args` array will not be `null`. (Exception ... if you call `main` from your own code with bogus args ...) Of course, args.length could be zero, or any of the args could be an empty string. But no `null` values are possible if `main` is invoked normally on JVM startup. – Stephen C Aug 22 '16 at 02:00
1 Answers
-1
            
            
        Here is a simple example Sam:
if (args.length > 0) {
    if (!args[0].isEmpty()) { 
        // Do whatever with arg[0] (the first argument)
    }
    if (!args[1].isEmpty()) { 
        // Do whatever with arg[1] (the second argument)
    }
}   
 
    
    
        DevilsHnd - 退職した
        
- 8,739
- 2
- 19
- 22
- 
                    1
- 
                    Then what about `args[2]`? Don't you think it would be helpful if you have used `for` loop instead of `if`? – Aug 22 '16 at 01:56
- 
                    
- 
                    
