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: what is wrong with my code

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what is wrong with my code

    when I create an instance of the ticket class and call the method print ticket, it comes up with this,
    sss.jpg

    this is my code
    import java.util.ArrayList;
    /**
     * lucky dip lottery ticket system.
     * 
     * @author (Daniel Prempeh) 
     * @version 1.1 2013
     */
    public class Ticket
    {
        //array list store instances of number class
        private ArrayList<Numbers> lottoticket;
        private int qty;
     
        public Ticket(int numOfLines)
        {
            lottoticket = new ArrayList<Numbers>();
            //needs a loop
            for(int i=0; i<qty; i++)
            {
                lottoticket.add(new Numbers());
            }
        }
     
        //print out ticket
        public void printTicket()
        {
            //printout header of ticket 
            System.out.println("******************************");
            System.out.println("**                          **");
            System.out.println("**         Ticket           **");
            System.out.println("**                          **");
            System.out.println("******************************");
            //detail
            //needs a loop
            lottoticket.get(0).print();
            //printout footer of ticket
            System.out.println("******************************");
        }
    }

    the fault occurs with lottoticket.get(0).print();
    and on the terminal it says...

    java.lang.indexoutofboundsexception: index 0, size 0

    at java.util.arraylist.rangecheck(arraylist.java:604)
    at java.util.arraylist.get(arraylist.java:382)
    at ticket.printticket(ticket.java:35)
    Last edited by Norm; January 10th, 2013 at 05:59 PM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,817
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: what is wrong with my code

    ava.lang.indexoutofboundsexception: index 0, size 0
    at java.util.arraylist.rangecheck(arraylist.java:604)
    at java.util.arraylist.get(arraylist.java:382)
    at ticket.printticket(ticket.java:35)
    If the arraylist is empty(size = 0) there is no first element(index = 0)

    Look at line 35 in the code to see why it expects there to be an element in the arraylist.
    How many elements were added to the arraylist? Add some println statements to print out the values of the variables used to control the loops and to show where the code is executing. Does the code in the loop ever execute?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code

    are you talking about does this peice of code executes
    public Ticket(int numOfLines)
    {
    lottoticket = new ArrayList<Numbers>();
    //needs a loop
    for(int i=0; i<qty; i++)
    {
    lottoticket.add(new Numbers());
    }
    }

    --- Update ---

    when I print it to the terminal it comes out with this and there are supposed to be 6 numbers in one row under

    ******************************
    ** **
    ** Ticket **
    ** **
    ******************************

    --- Update ---

    Quote Originally Posted by Norm View Post
    If the arraylist is empty(size = 0) there is no first element(index = 0)

    Look at line 35 in the code to see why it expects there to be an element in the arraylist.
    How many elements were added to the arraylist? Add some println statements to print out the values of the variables used to control the loops and to show where the code is executing. Does the code in the loop ever execute?
    now the problem is on lottoticket.get(0).print();, it is supposed to print me out a line of numbers

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what is wrong with my code

    You pass a parameter numOfLines into the instantiator but don't appear to use it. The for loop will never add an item to the array because the value of qty will be initialised to 0 and as i is also 0 the test i<qty will always be false. I am assuming from your second post that numOfLines = 6 and that the line should be for(int i=0; i<numOfLines; i++)

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,817
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: what is wrong with my code

    lottoticket.get(0).print()
    lottoticket is empty. There is no first element (0)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2012, 04:33 PM
  2. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 01:09 PM
  3. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM
  4. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM
  5. What's wrong with my code ?
    By mithani in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2010, 09:57 AM