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

Thread: Help with array code.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with array code.

    Very new to Java and am trying to create an array that has 5 integers and display them from first to last and last to first. What I have so far:

    public class arrayex {
     
     
    	public static void main (String[]args)
    	{
    	//create an array to hold 5 integers
       	int [] array = new int [5];
        int i=0;
    	for(i=0;i<5;i++);
    	System.out.println (array[i]);
        }
     
    }

    Here is the error message I am getting. not sure why.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at arrayex.main(arrayex.java:19)


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with array code.

    Check the semicolon after the loop...this will cause your loop to run, and only after completion the println will try to print...at which point i is equal to 5.

  3. The Following User Says Thank You to copeg For This Useful Post:

    beginnerjava (July 1st, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help with array code.

    1. You have declared an array that can hold 5 int values, but you haven't put any int values into it...
    2. Your 'for' loop has a misplaced semi-colon at the end of the 'for...' statement, so the loop does nothing and the print of the array item happens after the loop has finished - when index i is 5, which is past the end of the array.

    ETA: D'oh! copeg got there first...

  5. The Following User Says Thank You to dlorde For This Useful Post:

    beginnerjava (July 1st, 2011)

  6. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with array code.

    Fixed the semicolon and added the int values.

    [CODE] public static void main (String[]args)
    {
    //create an array to hold 5 integers
    int [] array = new int [5];

    int i=0;
    array[i] = i + 1;
    for(i=0;i<5;i++)

    System.out.println (array[i]);
    }

    }[CODE]

    Now, I get this output with only the number 1 and the rest zeros.

    output:

    1
    0
    0
    0
    0

    Process completed.

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with array code.

    Yes, that is what your code says to do: Put a value in element 0 of the array.
    int i=0;   // set the value of i to 0
    array[i] = i + 1;  // add 1 to i (1+0 = 1) and put that in element 0 of array
    What do you want your code to do?

  8. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with array code.

    @ Norm, I want the code to display 5 integers from first to last then last to first.

  9. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help with array code.

    If you want your array to have 5 integers in it, you have to put 5 integers into it. The compiler can't read your mind. You've told it to put one integer into position 0, so it put one integer into position zero.

    I suggest you use one loop to put the integers into the array and a couple of loops to print them out again in first-to-last and last-to-first order.

Similar Threads

  1. Duplicating array in C code
    By FearTheCron in forum Java Native Interface
    Replies: 1
    Last Post: April 6th, 2013, 09:33 PM
  2. Array code not producing, thoughts?
    By r19ecua in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2011, 05:37 PM
  3. Array code problem Please help fairly easy
    By LOPEZR in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 10:51 AM
  4. Array Code Help
    By whattheeff in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 21st, 2011, 04:44 PM
  5. Need help with array code
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2010, 10:54 PM