0

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)
Schuld
  • 7
  • 4
  • 1
    What does your isInteger() look like? What is the error you are getting in the log? – Chris Stillwell Feb 24 '15 at 21:05
  • 1
    As @ChrisS said, provide the stack trace of your error, and also try to include your isInteger method – Tarik Feb 24 '15 at 21:07
  • added my isInteger method in there. it just crashes, will put up error code later – Schuld Feb 24 '15 at 21:28
  • im sorry, here's the error i got 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) – Schuld Feb 24 '15 at 21:40
  • @Schuld please add it to your question, so that your question will be more clear, and later people may benefit from it ;) – Tarik Feb 24 '15 at 21:43
  • @Tarik just done that :), any idea why it crashes? – Schuld Feb 24 '15 at 21:50
  • @Schuld I added my answer below – Tarik Feb 24 '15 at 22:15

2 Answers2

2

Welcome to Stack Overflow. The issue can be read from the stack trace. And this is what you have.

if (isInteger(args[i]) == false) {
      System.out.print("Error: Argument type mismatch");
    }
int a = Integer.parseInt(args[i]);

Note that there is no "else" after the "if" so the value of "a" is attempted to be calculated again even though the args[i] is not an Integer.

I hope you can figure how to resolve this with this hint.

Khanna111
  • 3,627
  • 1
  • 23
  • 25
0

This is normal, lets say you have args[i] equal to "someString", then when you call you isInteger() function that will return false. Then, when you said isInteger(args[i]) == false that mean false == false which is true. and the System.out.print("Error: Argument type mismatch"); will execute properly.

After that, when you call int a = Integer.parseInt(args[i]); without catching the NumberFormatException it's normal to that your application crash with that exception error.

To handle that, you can just add an else block like suggested in Khanna111's answer, like that:

 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");
    }
    else {
    int a = Integer.parseInt(args[i]);
    num += a;
    }
  }
  System.out.println(num);
}

Or you can use a continue, which allows the execution to jump to the next element in the for loop and continues on (updated, Thanks to Tom's comment):

 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");
      continue;
    }
    int a = Integer.parseInt(args[i]);
    num += a;
  }
  System.out.println(num);
}
Tarik
  • 4,961
  • 3
  • 36
  • 67