Why won't this work? after being compiled.
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)); } }
Untitled.jpg
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Why won't this work? after being compiled.
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)); } }
Untitled.jpg
Last edited by NewMember; February 18th, 2013 at 01:27 AM. Reason: indentation
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:
NewMember (February 18th, 2013)
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.
If you don't understand my answer, don't ignore it, ask a question.
NewMember (February 18th, 2013)
Alright thanks for the quick response, it seems to be working.