so far i've got this, im trying to use command lines in java. And I want to print an error when the input isn't an int.
private static void add(String[] args) {
if (args.length == 1) {
System.out.print("Error: Argument count mismatch");
}
int num = 0;
for (int i = 1;i < args.length;i++) {
if (isInteger(args[i]) == false) {
System.out.print("Error: Argument type mismatch");
}
int a = Integer.parseInt(args[i]);
num += a;
}
System.out.println(num);
}
the 2nd if statement is the part where I want to print an error if the input isn't int, I have a isInteger method. But my program crashes instead or printing the error.
edit: this is my isInteger method
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
}
return true;
}
so there shouldn't be a problem here.
edit2: here is the error that I have gotten
java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at MyCLIParser.add(MyCLIParser.java:46)
at MyCLIParser.main(MyCLIParser.java:10)