Why won't this work? after being compiled.
Code :public class Add { public static void main(String[] args) { int a = Integer.parseInt(args[0]), b = Integer.parseInt(args[1]); System.out.println(a + " + " + b + " = " + (a + b)); } }
Attachment 1792
Printable View
Why won't this work? after being compiled.
Code :public class Add { public static void main(String[] args) { int a = Integer.parseInt(args[0]), b = Integer.parseInt(args[1]); System.out.println(a + " + " + b + " = " + (a + b)); } }
Attachment 1792
The error means String[] args is empty but you're trying to get values from it. Even if String[] args contained something that could be converted to an integer, you would still receive compiler errors because you cannot initialize variables on the same line using your approach. That is:
The code should test if the args array has anything in it before trying to use it. Use the array's length attribute.
If args.length is >= 2 then the code can get args[0] and args[1]
If the length is < 2 print out an error message and exit.
Alright thanks for the quick response, it seems to be working.