0

I need to take in command prompt arguments, and use them to construct an object. I am taking a number of people and the boat size.

public RiverCrossingPuzzle(int numEach, int boatSize)

Creates a RiverCrossingPuzzle with numEach number of cannibals and numEach number of missionaries with a boat that can carry up to boatSize number of missionaries/cannibals. For example, the input can be "-n", "4", "-b", "3"

Should I do it like this:

this.numEach = Integer.parseInt(args[2]);
this.boatSize = Integer.parseInt(args[4]);

Or like this:

int numEach = Integer.parseInt(args[2]);
int boatSize = Integer.parseInt(args[4]);
this.boatSize = boatSize;
this.numEach = numEach;
2
  • Have you tried doing both? Is there a difference? Why use 4 lines of code if you can do the same in 2, as easy to understand lines of code? Commented Oct 15, 2014 at 6:19
  • Your first example is fine. But, as you specify parameters, I guess '-b 3 -n 4' is also a valid input. You have to iterate with e.g. a for loop over args, detect the indexes of your parameters (-n, -b) and take their values at index+1. Also take care of the edge cases if e.g. the entered parameters aren't valid. Commented Oct 15, 2014 at 6:21

3 Answers 3

2

I would read the and parse the command line arguments before calling the constructor.

public static void main (String[] args)
{
    int numEach = 0; // or some other default value that makes sense
    int boatSize = 0; // or some other default value that makes sense
    if (args.length > 1)
        numEach = Integer.parseInt(args[1]);
    if (args.length > 3)
        boatSize = Integer.parseInt(args[3]);
    RiverCrossingPuzzle puzzle = new RiverCrossingPuzzle (numEach, boatSize);
}

You should also add exception handling in case the command line arguments cannot be parsed at int.

BTW, based on your "-n", "4", "-b", "3" example, you should be reading args[1] and args[3].

Sign up to request clarification or add additional context in comments.

Comments

0

Something you might want to take a look into, also, is that of your program arguments. You are expecting a user to start the program with the arguments of -n 4 -b 3, that could not always be the case. The user could start the program with -n and -b the other way around. You always have to assume that the user will try to break your application (even though this is probably just an exercise, it's a good habit to get in to). Now how could you resolve this? The most simple way would be to go through and see where the indicators -n and -b are in the program arguments, as such:

public static void main(String[] args) {
    int nIndex = -1; //Just some value that clearly will never resolve in an array, suggesting it has not been found
    int bIndex = -1;
    for(int i = 0; i < args.length; i++)
        if (args[i].equals("-n"))
            nIndex = i + 1; //The next index will correspond to the value of -n
        else if (args[i].equals("-b"))
            bIndex = i + 1; //The next index will correspond to the value of -b
    int boatSize, numEach;
    if(nIndex != -1 && bIndex != -1 && nIndex < args.length && bIndex < args.length) {
        /*Check that they were both parsed AND that neither of them are outside of the array range
         *(such as starting with "-n 5 -b" and no value for b
         */
        boatSize = Integer.parse(args[bIndex]);
        numEach = Integer.parse(args[nIndex]);
    }

}

Comments

0

I would follow the try to parse command line approach because it also handles invalid input:

public static void main (String[] args){

    RiverCrossingPuzzle puzzle;
    try{
        int numEach= Integer.parseInt(args[1]);
        int boatSize= Integer.parseInt(args[3]);
        puzzle = new RiverCrossingPuzzle (numEach, boatSize);
    }catch(NumberFormatException e){
        e.printStackTrace(); // maybe log it?
        System.out.println("Invalid command line arguments passed in!! You should pass arguments such as : -n NUMBER -b NUMBER");
        return; 
   }
}

1 Comment

It would be much cleaner if the variables declarations and the call to the constructor were inside the try block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.