Make a command line application with parameter

I made a command line application which uses parameters. So I try to make a system like the linux command.
So I have 4 parameters, initialized to a default value, used if wrong or none parameter are given. The user can modify those parameter by calling associate options "-options".

   public static void main(String [] args) {
String locParameter = "centralOffice";
String dirResult = "//home//"+System.getProperty("user.name")+"//Documents//ResultMyApp";
int periodParameter = 24;
String counterParameter = "ALL";
int i = 0;

for (String s: args) {
switch (s) {
case "-o":  if (args[i+1].equalsIgnoreCase("remote")){
locParameter = "remoteOffice";
break;
} else if (args[i+1].equalsIgnoreCase("central")){
locParameter = "centralOffice";
break;
} else {
System.err.println("Unknow parameter for location, default value: centralOffice");
break;
}
case "-p":  dirResult = args[i+1];
break;
case "-kpi": counterParameter = args[i+1].toUpperCase();
break;
case "-t":  try {
periodParameter = Integer.parseInt(args[i+1]) ;
} catch(NumberFormatException e){
System.err.println("Error parsing parameter, default value 24 hours");
} finally {
break;
}
}
i++;
}

System.out.println("parameter choosed: " + locParameter + ", " + periodParameter + ", " + counterParameter );}


So I am wondering, is this ok? Of course it works but maybe it is not the easiest way to do it or the most optimized code.

• Unless you are doing it as an exercise, it would make more sense to use an existing library such as jcommander or equivalent. – assylias Feb 5 '16 at 10:21
• It is not an exercise, I searched a bit on google before doing this but I found only people speaking about doing their own code. Thanks I will look at this. – Charly Roch Feb 5 '16 at 10:22
• Another one is jopt simple - their home page lists alternatives. – assylias Feb 5 '16 at 10:24
• jcommander is great, but you have to add your own fix against program -- or something like that - the last time I used it, running it with -- as arguments crashed the library – Pimgd Feb 5 '16 at 10:26
• Why does dirResult have double-slashes? – 200_success Feb 6 '16 at 8:10

Parameter validation is sorely missing. For example, if you call this with -o as last argument (no more arguments), the program will crash with an array index out of bounds exception and a stack trace. Getting a lengthy stack trace for invalid output is not very UNIX-y ;-)
The finally in the exception handling is not necessary and a bit confusing. I suggest simply moving the break after the catch.