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.

Results 1 to 12 of 12

Thread: how to give inputs to my simple java program

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile how to give inputs to my simple java program

    Hi all,

    This is pokuri am in starting stage of java . Now what the error is i written a simple addition code compilation is successful . In run am getting exception "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at AddNum.main(AddNum.java:17)"

    And my code is
    import java.lang.*;
     
    class AddNum
    {
        public static void main(String arg[])
        {
            int a;
            int b;
            int c;
     
            a=Integer.parseInt(arg[1]);
            b=Integer.parseInt(arg[2]);
            c=Integer.parseInt(arg[3]);
            c=a+b;
            System.out.println("total amount"+c);
        }
    }
    please give me solution for that exception and also how to give inputs to both the variables .
    Last edited by helloworld922; January 5th, 2011 at 12:19 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to give inputs to my simple java program

    Java array indices start at 0, not 1. Perhaps that's your problem?

    Also, in the future please surround your code with highlight tags, I've modified your post this time to reflect that.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: how to give inputs to my simple java program

    tanx for your reply but am getting same thing again

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to give inputs to my simple java program

    Are you sure you're passing the correct number of parameters to your program? The syntax for calling should be something like this:

    java AddNum 1 2 3

    edit: removed .class
    Last edited by helloworld922; January 5th, 2011 at 05:30 PM.

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: how to give inputs to my simple java program

    again error "Exception in thread "main" java.lang.NoClassDefFoundError: AddNum/class"

  6. #6
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: how to give inputs to my simple java program

    Try "java AddNum 1 2 3"

    FYI: In your code, you are assigning c to the third arg, but then immediately overwriting it with the addition of the first two args

  7. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: how to give inputs to my simple java program

    you need to define the class as public.

    public class AddNum{
    //...
    }

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  8. #8
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: how to give inputs to my simple java program

    No he doesn't (Not saying he shouldn't, but that isn't causing an error). What he has will work fine.

    The reason for his last error is because helloworld922 erroneously put a .class in his advice

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to give inputs to my simple java program

    oops, my bad. I've changed my post to remove the .class

  10. #10
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: how to give inputs to my simple java program

    Instead of doing it one by one, you can pass in any amount of integers
    public class AddNum
    {
        public static void main(String arg[])
        {
            int total=0;
            for(int i=0;i<arg.length ;i++){
                total+=Integer.parseInt( arg[i] );
            }
            System.out.println(total);
        }
    }

  11. #11
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: how to give inputs to my simple java program

    I hope you have changed your code from,
    a=Integer.parseInt(arg[1]);
    b=Integer.parseInt(arg[2]);
    c=Integer.parseInt(arg[3]);
    to
    a=Integer.parseInt(arg[0]);
    b=Integer.parseInt(arg[1]);
    c=Integer.parseInt(arg[2]);
    This is because the arguments array is 0 index based. Now when you run the code with java AddNum 1 2 3
    arg[0] = 1 
    arg[1] = 2
    arg[2] = 3
    You can use any variable other than c for you addition, so that you can use c later if needed. Because you are overriding value of c as soon as its received from arguments, which doesn't make much sense. After this I don't think there should be any problem.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Location
    Lolo, Mt
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to give inputs to my simple java program

    I'm not sure i follow what your trying when i was at the beginner stage they taught me a class that could be used to generate input. i see that your program is trying to add three integers
    the package is called hsa.Stdin
    to add integers call for input for the three use "int value = Stdin.readInt();
    import hsa.Stdin;
    public class Junk
    {
        public static void main (String[] args)
        {
            int a, b, c;
            System.out.print ("A: ");
            a = Stdin.readInt ();
            System.out.print ("\nB: ");
            b = Stdin.readInt ();
            System.out.println ();
            c = a + b;
            System.out.println ("Answer = " + c);
        }
    }
    this was what i was tought and feel that this is the easiest way to have it done

Similar Threads

  1. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM
  2. Need simple JAVA program fixing ASAP
    By theviper in forum Paid Java Projects
    Replies: 1
    Last Post: April 14th, 2010, 10:59 AM
  3. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  4. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  5. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM