LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java newbie Array -out of bounds exception (https://www.linuxquestions.org/questions/programming-9/java-newbie-array-out-of-bounds-exception-435111/)

dflan98783 04-14-2006 10:27 AM

Java newbie Array -out of bounds exception
 
I was tasked to write a program that prompts for 10 numbers, averages them and counts the occurance of the numbers above the average.

I followed examples provided and wrote the following program, but when I run it I get array out of bounds. I understand what the error is, just can't seem to correct it.

Can someone please assist me?

Thanks


package flanagan_ch5_p1;
import javax.swing.JOptionPane;
/**
* <p>Title: Chapter 5 PE 1</p>
*
* <p>Description: Analyzing input with arrays</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: NOVA, ELI, ITP 120, Spring 06</p>
*
* @author Debora A. Flanagan
* @version 1.0
*/
public class flanagan_ch5_p1 {

/**Main method*/
public static void main(String[] args) {
final int TOTAL_NUMBERS = 10;
int [ ] numbers = new int[ TOTAL_NUMBERS ];
int sum = 0;

//Request numbers from user and convert to interger
for (int i = 0; i < TOTAL_NUMBERS; i++){
String numString = JOptionPane.showInputDialog(null, "Enter a number",
"P.E. 5.1 Input", JOptionPane.QUESTION_MESSAGE);

//Convert string to integer
numbers [ i ] = Integer.parseInt(numString);
}

//Compute their average
sum += numbers [TOTAL_NUMBERS];
int average = sum / 10;

//Find the numbers above average and count how many
int numOfAbove =0;
int count = 0;
for (int i =0 ; i < TOTAL_NUMBERS; i++) {
if (numbers [i] > average) count++;
}

//Prepare the results
String output = "The numbers entered were ";
for (int i = 0; i < TOTAL_NUMBERS; i++) {
output += numbers [i] + " ";
}


output += "\nThe average of the numbers is " + average;
output += "\nThe occurance count of the numbers above average is " + count;

//Display the result
JOptionPane.showMessageDialog(null, output, "P.E. 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
}
}

Nylex 04-14-2006 10:33 AM

Quote:

Originally Posted by dflan98783
sum += numbers [TOTAL_NUMBERS];

The array out of bounds exception is thrown when you try to use an index greater than (n - 1), where n = number of elements in the array. You've declared your array to hold TOTAL_NUMBERS elements and in the line above, you're trying to access an index greater than the maximum for that array. Array counting starts from zero, hence for 10 elements, the index of the last element will be 9.

Edit: you really want a loop to be calculating that sum:

Code:

int sum = 0;

for(int i = 0; i < TOTAL_NUMBERS; i++)
{
  sum += numbers[i];
}

double avg = sum / TOTAL_NUMBERS;

Also, in future could you use code tags? They make code easier to read!

crabboy 04-14-2006 10:41 AM

sum += numbers [TOTAL_NUMBERS];

That line is your problem.

You are creating an array that can be index from 0-9, you are indexing the array with a 10.

dflan98783 04-14-2006 10:43 AM

Thank you for the quick response. I do apologize about the code tags, not sure how to use them.


All times are GMT -5. The time now is 01:05 PM.