Java: Check if command line arguments are null

后端 未结 6 1726
太阳男子
太阳男子 2020-11-28 06:34

I am looking to do some error checking for my command line arguments

public static void main(String[] args)
{
    if(args[0] == null)
    {
        System.ou         


        
6条回答
  •  一向
    一向 (楼主)
    2020-11-28 06:59

    if i want to check if any speicfic position of command line arguement is passed or not then how to check? like for example in some scenarios 2 command line args will be passed and in some only one will be passed then how do it check wheather the specfic commnad line is passed or not?

    public class check {
    
    public static void main(String[] args) {
    if(args[0].length()!=0)
    {
    System.out.println("entered first if");
    }
    if(args[0].length()!=0 && args[1].length()!=0)
    {
    System.out.println("entered second if");
    }
    }
    }
    

    So in the above code if args[1] is not passed then i get java.lang.ArrayIndexOutOfBoundsException:

    so how do i tackle this where i can check if second arguement is passed or not and if passed then enter it. need assistance asap.

提交回复
热议问题