Hi, I'm trying to input a random number of Strings by passing them in as args[]. When I try to use a for loop to determine when there is no more arguments left it gives me an "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10" error. I've tried looking for a null spot at the end but that doesn't seem to work. I've resorted to passing in two null Strings to determine where the end is. My question is if there is an easier way to do this, such that I don't need to pass anything special in with my args.

//Starts the main Method
    public static void main(String[] args) {
        Main main = new Main();
        String ip = args[0];
        String username = args[1];
        String password = args[2];
        String pathname = "C:/Automation/Scripts/test_files/smoke/";
        String filename = pathname + "fw4.pdf";
        ArrayList keys = new ArrayList();
        ArrayList values = new ArrayList();
        main.getattrs(args[0],args[1],args[2],args[3]);
        main.init();
        main.connect(ip,username,password);
        
        
        //Sets the keys to be set
        for(int count = 4;!args[count].equals("null");count = count+2){
            int index = 0;
            keys.add(args[count]);
            index++;
        }
        
        //Sets the key values to be set
        for(int count = 5;!args[count].equals("null");count = count+2){
            int index = 0;
            values.add(args[count]);
            index++;
        }
        
        //Creates a job to change the values of
        byte[] JOBID = main.makeJob(ip,"hold",filename);
        
        //Checks to make sure the keys and values arrays have the same number
        //of elements
        int keylength = keys.size();
        int valuelength = values.size();
        if(keylength != valuelength){
            System.out.println("Error in inputed keys and values!");
        }
        
        //Changes the keys and values of a job
        System.out.println("Changing "+keylength+" attributes!");
        for(int index = 0;index < keylength;index++){
            main.setJobAttributes(JOBID,keys.get(index).toString(),values.get(index).toString());
        }
        main.disconnect();
    }
    //End main method

Thanks

Recommended Answers

All 2 Replies

>I try to use a for loop to determine when there is no more arguments
I do not see a reason to use for loop to determinate the number of command line arguments passed to application
Simples thing would be

commandLineArguments = args.length;

. This will also help you see if the user realy provided minimum of 3 argumants requered for IP, username and password. If this not fullfiled you just display error message and close application.

PS: You should clean your code it is realy messy...

Thank you for posting, your solution worked great. I know my codes a little messy. I just started learning how to program not to long ago, so I still have a lot to learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.