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

Thread: Need help with array code

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Need help with array code

    Hi I'm just beginning to learn and work with arrays and I was going to try a program that ask the user to enter 8 hourly temperatures and let the program output these temperatures.

    When I run the program and enter the 8 temperatures this is what my prompt says:

    'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at TemperatureArray.main(TemperatureArray.java:18)'

    I have never seen this before.. my code compiles fine but here it is also:

    import java.util.*;
     
    public class TemperatureArray
    {
    	static Scanner console = new Scanner(System.in);
     
    	public static void main(String[] args)
    	{
    		int[] temp = new int[8];
     
    		int avg;
    		int index;
     
    		System.out.println("Enter 8 numbers: ");
     
    		for (index = 0; index < temp.length; index++)
    			temp[index] = console.nextInt();
    			System.out.print(temp[index] + " ");
     
    	}
    }


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

    Default Re: Need help with array code

    You forgot to put the braces around the for loop. Also, as a good programming practice, don't declare for loop counters outside of the for loop.

    // ex.
    for (int index = 0; index < temp.length; index++)
    {
         temp[index] = console.nextInt();
         System.out.print(temp[index] + " ");
    }

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

    n00bprogrammer (April 6th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help with array code

    Thanks a lot for the tip and correction.. now I have to figure out how to get the output to print after I type all of the input lol

    It's a shame I've had to pay $80 for a book full of errors. My book was saying to print an array do a for loop like:

    for (index = 0; index < array.length; index++)
         System.out.print(array[index] + " ");

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

    Default Re: Need help with array code

    That's actually correct. Again, from a programming style I prefer to put the index declaration inside the for loop as a second check just to make sure I don't use that variable anywhere else (at least not without re-declaring it).

    If you want to have the display after, simply use two loops: one to get the input from the user, and the second loop to print out the display.

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM