Results 1 to 8 of 8
  1. #1
    jeswanthvijay is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExcep

    I am new to java programming doing a self study , when i try to run program i get a exception in thread main

    class progegif
    {
    public static void main(String args[])
    {
    int num=Integer.parseInt(args[0]);
    if(num>=1 & num<=3)
    {
    if(num==1)
    System.out.println("the number =1");
    if(num==2)
    System.out.println("the number =2");
    if(num==3)
    System.out.println("the number =3");
    }
    else
    System.out.println("the number is nt between 1 n 3");
    }
    }

  2. #2
    drewtrcy is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default

    I am new at this also but if I am reading your code right, what is the value of num? I do not see it's value anywhere. I could be wrong though with my thoughts on that.

  3. #3
    jeswanthvijay is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by drewtrcy View Post
    I am new at this also but if I am reading your code right, what is the value of num? I do not see it's value anywhere. I could be wrong though with my thoughts on that.
    Thanks for ur help . but its a keyboard input program when i run the prog it will ask me to type the value for num= Tats how it works

  4. #4
    jeswanthvijay is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by drewtrcy View Post
    I am new at this also but if I am reading your code right, what is the value of num? I do not see it's value anywhere. I could be wrong though with my thoughts on that.
    I compiled the prog it works well but when i try to run it show me error as

    C:\java>java progegif
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at progegif.main(progegif.java:5)

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default

    Do you understand what is implied by this line of your code?
    Java Code:
    int num=Integer.parseInt(args[0]);
    Or more specifically, what is args[0]?

    Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

    db

  6. #6
    jeswanthvijay is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Do you understand what is implied by this line of your code?
    Java Code:
    int num=Integer.parseInt(args[0]);
    Or more specifically, what is args[0]?

    Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

    db
    args[] is an user defined array variable of type string.Each input given on the command line is stored is automatically stored in array

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by jeswanthvijay View Post
    Each input given on the command line is stored is automatically stored in array
    Then,
    Java Code:
    java progegif
    is the right way to run your program considering that it's expecting an argument there?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    jeswanthvijay is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Exactly you are right , My prog doesn't show any error ....... Awesome
    Thank u so much

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 AM

Posting Permissions

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