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 5 of 5

Thread: Why is this code giving me an ArrayIndexOutOfBoundException?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Why is this code giving me an ArrayIndexOutOfBoundException?

    public class CreditCardTest1
    {
    public static void main(String args[])
    {
    String creditCard = args[0].toUpperCase();
    if(creditCard.equals(AllowedCreditCard.VISA.name() ))
    {
    System.out.println("Your credit card" + args[0] + "is accepted.");
    }
    else if(creditCard.equals(AllowedCreditCard.MASTER_CARD .name()))
    {
    System.out.println("Your credit card" + args[0] + "is accepted");
    }
    else if(creditCard.equals(AllowedCreditCard.AMERICAN_EX PRESS.name()))
    {
    System.out.println("Your credit card" + args[0] + "is accepted");
    }
    else
    {
    System.out.println("Sorry we do not accept the credit card" + args[0] +
    "at this time");
    }
    }
    }
    /*class AllowedCreditCard
    {
    protected final String card;
    public final static AllowedCreditCard VISA = new
    AllowedCreditCard("VISA");
    public final static AllowedCreditCard MASTER_CARD = new
    AllowedCreditCard("MASTER_Card");
    public final static AllowedCreditCard AMERICAN_EXPRESS = new
    AllowedCreditCard("AMERICAN_EXPRESS");
    private AllowedCreditCard(String str)
    {
    card = str;
    }
    public String getName()
    {
    return card;
    }
    }*/

    enum AllowedCreditCard
    {
    VISA, MASTER_CARD, AMERICAN_EXPRESS
    }


  2. #2
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Why is this code giving me an ArrayIndexOutOfBoundException?

    Why are you using an array called args[0]?

    Wrap your code in the [CODE] blocks and format it so its easier to read and and understand.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Why is this code giving me an ArrayIndexOutOfBoundException?

    @StephenCoyle You can pass strings to a main method from the command line when you run the program. This is why main always begins
    main(String args[])

    Post the error message you saw when you tried to run the program. Array out of bounds means you tried to reference an element that exceeded the array size/boundaries.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Why is this code giving me an ArrayIndexOutOfBoundException?


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Martina3.CreditCardTest1.main(CreditCardTest1.java :7)
    Java Result: 1

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is this code giving me an ArrayIndexOutOfBoundException?

    The index used with the array at line 7 is past the end of the array. The array appears to be empty because there is no first element (index = 0)
    The code should use an if statement to test the length of the array to make sure there are elements in the array.
    If not either print out a message telling the user the problem
    or give the array a default value with an assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    TheCoder (March 6th, 2013)

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 2013, 07:29 AM
  2. Almost Giving up :((
    By amakuzaki in forum Loops & Control Statements
    Replies: 21
    Last Post: June 21st, 2012, 11:33 AM
  3. giving arraylist name
    By iddheepak in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2011, 02:08 PM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. Replies: 0
    Last Post: March 5th, 2010, 01:32 AM

Tags for this Thread