Skip to content

Instantly share code, notes, and snippets.

@mkalkov
Last active May 31, 2017 11:01
Show Gist options
  • Save mkalkov/186b16d6f278d157213a4910fff9c51a to your computer and use it in GitHub Desktop.
Save mkalkov/186b16d6f278d157213a4910fff9c51a to your computer and use it in GitHub Desktop.
Simple example to demonstrate issue
package se.sjv.jorden.datachange;
import com.google.common.base.Preconditions;
public class MyParser {
public static void main(final String[] args) {
final MyParser myParser = new MyParser();
Preconditions.checkArgument(args.length == 1, "Incorrect number of arguments.");
// do something before parsing
myParser.parse(args[0]);
// do something after parsing
}
private void parse(final String input) {
final String[] tokens = input.split(";");
Preconditions.checkArgument(tokens.length == 3);
try {
System.out.println("First parameter: " + parseFirstParam(tokens[0]));
System.out.println("Second parameter: " + parseSecondParam(tokens[1]));
System.out.println("Third parameter: " + parseThirdParam(tokens[2]));
} catch (final IllegalArgumentException e) {
System.err.println("Error while parsing parameters: " + e.getMessage());
}
}
private String parseFirstParam(final String string) {
return string.trim().toLowerCase();
}
private String parseSecondParam(final String string) /* throws IllegalArgumentException */ {
final int parsedInt = Integer.parseInt(string); // this may throw NumberFormatException,
// which is a subclass of IllegalArgumentException
return String.format("%d st", parsedInt * 100);
}
private String parseThirdParam(final String string) {
return string.trim().toUpperCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment