Results 1 to 4 of 4

Thread: Confused

  1. #1
    coldfire is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default Confused

    Hi, I have just started learning Java and I have a question.

    Using Eclipse, I am trying to run this:

    Java Code:
    public class AddNumbers{
      public static void main(String[] args) {
        System.out.println("Addition of two numbers!");
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]);
        int sum = a + b;
        System.out.println("Sum: " + sum);
      }
    }
    But when I run it, I get the error:

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at AddNumbers.main(AddNumbers.java:4)
    I am confused as to why this is happening, can anyone help?

    I know it's probably going to be some stupidly obvious thing that i'm forgetting, but i'm just a noob! :P

    Thanks!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to run this code with two command line parameters. But seems to me, you don't have use any.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Include arguments

    When you run the program, you have to include two arguments (the numbers you want to add). For example, if you want to add 3 & 4, you would specify:

    Java Code:
    java AddNumbers 3 4
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. I am sooo confused... Please help!
    By switch09 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 12:39 AM
  2. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM
  3. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM
  4. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 04:52 AM
  5. confused code
    By creativehacker in forum New To Java
    Replies: 6
    Last Post: 07-02-2007, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •