LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2006, 11:29 PM   #1
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Rep: Reputation: 15
Question Array index out of bounds exception: newbie = [


I'm writing a program for a java course i'm taking, I am to set up an array that reads in (input from user or not, i've no idea because it's not specified so I assume not) - I have everything working fine, except when I run the program i'm getting this output...

Height 0 = 140
Height 1 = 130
Height 2 = 132
Height 3 = 154
Height 4 = 200
Height 5 = 201
Height 6 = 190
Height 7 = 182
Height 8 = 123
Height 9 = 167
Your average is: 161
Above average: [I@fd54d6
Above average: [I@fd54d6
Above average: [I@fd54d6
Above average: [I@fd54d6
Above average: [I@fd54d6
java.lang.ArrayIndexOutOfBoundsException
at Heights.main(Heights.java:24)

The top part is correct, including the average - but the above average part isn't = [, I can't seem to fix it either. Sorry for the denseness.

Here's my code.

Code:
// The "Heights" class.
public class Heights
{
    public static void main (String [] args)
    {

        // declare , initialize array
        int avg;
        int total = 0;
        int [] heightsArray = {140, 130, 132, 154, 200, 201, 190, 182, 123, 167};
        // print heights
        for (int i = 0 ; i < 10 ; i++)
        {
            System.out.println ("Height " + i + " = " + heightsArray [i]);
            total += heightsArray [i];
        }
        avg = total / 10;
        System.out.println ("Your average is: " + avg);

        int numOfAbove = 0;
        int count = 0;
        for (int i = 0 ; i < avg ; i++)
        {
            if (heightsArray [i] > avg)
            
                System.out.println ("Above average: " + heightsArray);
            }



        } // main method
    } // Heights class
Any and all help is appreciated, and will be credited in my program.

Last edited by ohfaney; 04-30-2006 at 11:38 PM.
 
Old 04-30-2006, 11:43 PM   #2
redhatrosh
Member
 
Registered: May 2004
Location: Student of University of Mumbai, Maharastra State, India
Distribution: Redhat Linux 9.0, Knoppix LIVE CD, Ubuntu Live CD, Kubuntu Live CD
Posts: 483

Rep: Reputation: 30
There is a minor logical error
Code:
avg = total / 10;
        System.out.println ("Your average is: " + avg);

        int numOfAbove = 0;
        int count = 0;
        for (int i = 0 ; i <avg ; i++) //variable "i" used for index of the elements in the array heightsarray
should actually be

Code:
avg = total / 10; //Calculate the "AVERAGE HEIGHT"
       System.out.println ("Your average is: " + avg);

int numOfAbove = 0;
        int count = 0;
        for (int i = 0 ; i < 10; i++) // Limit the counter the size of heights array for compaing values :)
            {
            if (heightsArray [i] > avg)
            
                System.out.println ("Above average: " + heightsArray[i]); //Print the one which is higher than the average (you missed out the [i], the element index
            }
Compare the value of the elements of the array to find the above average ones

The correct program should give you the output

Code:
Height 0 = 140
Height 1 = 130
Height 2 = 132
Height 3 = 154
Height 4 = 200
Height 5 = 201
Height 6 = 190
Height 7 = 182
Height 8 = 123
Height 9 = 167
Your average is: 161
Above average: 200
Above average: 201
Above average: 190
Above average: 182
Above average: 167

Last edited by redhatrosh; 04-30-2006 at 11:44 PM.
 
Old 04-30-2006, 11:45 PM   #3
ohfaney
LQ Newbie
 
Registered: Apr 2006
Location: Harlow, Essex, England
Posts: 28

Original Poster
Rep: Reputation: 15
Brilliant! = ] thanks for the help, it had me stumped and it's far too late to think lol.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Java newbie Array -out of bounds exception dflan98783 Programming 3 04-14-2006 10:43 AM
index of an element in the array ? thelonius Programming 1 09-24-2005 12:41 PM
help createing exception class from base STL exception qwijibow Programming 4 04-20-2005 05:23 AM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 11:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration