0

i've got the following program: i have a jar file which is getting 4 parameteres from main method :input file,order by, sort direction, output file. The input file is the file where information will be read , order by is sorting by (test,runs , etc), sort direction (asc, desc) and outputfile where information will be saved. Here is my code in main method :

      if (args.length > 0)
        {
            if (args[0].isEmpty())
            {
                System.out.println("Enter input file");
                System.exit(1);
            }
            else
            {
                args[0] = inputFile;
            }
            if (args[1].isEmpty())
            {
                System.out.println("Enter order by");
                System.exit(1);
            }
            else
            {
                args[1] = orderBy;
            }
            if (args[2].isEmpty())
            {
                System.out.println("Enter sort direction");
                System.exit(1);
            }
            else
            {
                args[2] = sortDirection;
            }
            if (args[3].isEmpty())
            {
                System.out.println("Enter output file");
                System.exit(1);
            }
            else
            {
                args[3] = outputFile;
            }

        }
        else
        {
            System.out.println("Must enter parameteres!");
            System.exit(0);
        }

          SaveInfoManager.SaveInFile(inputFile, sortDirection, orderBy, outputFile);

And i have a following error: ArraysIndexOutOfBoundsException when i do not enter some argument. Example i do not enter the 3rd argument which is sort direction and i want to print message that is not entered. But i do not understand why is throwing that exception even i make a check. Can you tell me where is my fault and how i can do it properly ?

2
  • 1
    The args array has the size of the number of provided arguments. You'd better check the size of the array to know how many were provided. Commented Aug 16, 2018 at 7:32
  • You checked if args.length > 0 before accessing any args. That tells you args[0] is within bounds, but that's all. You can't safely access args[1] without checking args.length > 1, or args[2] without checking args.length > 2, etc. Commented Aug 16, 2018 at 7:34

1 Answer 1

1

args[n].isEmpty() checks whether the string that is contained in position n of the array is empty, which results in ArraysIndexOutOfBoundsException if the array has less than n elements.
In order to check the length of the array you need to use

if(args.length == 4){ ... }
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, but how i can check the specific 2nd parameter for example. And tell the user it must be filled
@George You could use if(args.length<2)... to see if there are less than 2 parameters. Checking if a specific parameter is missing isn't really possible since you only use the order of the arguments to indicate which one is which. If you want to check specific parameters I suggest that you add flags to the arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.